| Author |
timezone information getting lost in java.sql.date
|
Sharadha Krishnamurthi
Greenhorn
Joined: Jun 30, 2005
Posts: 1
|
|
Hi, Please refer the following code for the quetion. java.sql.Date today = new Date(calendar_today.getTime().getTime()); Calendar_today is in a timezone different from the JVM's timezone, when i create a sql.date out of it,using the code above, date is created in the current time zone.how to create a date for a given(calendar_today's) time zone. Regards, Sharadha
|
The happiest of people don't necessarily have the best of everything;<br />they just make the most of everything that comes along their way
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
Working with timezones can be confusing in Java. java.util.Date and java.sql.Date (which is a subclass of java.util.Date) do not contain timezone information at all. The API documentation for java.sql.Date says: "A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT." So the value in a java.sql.Date is always in the GMT timezone. However, if you print it to the screen with System.out.println(...), then it will be displayed as a time in the default timezone. If you use e.g. SimpleDateFormat to convert a date to a string, then you can use setTimeZone(...) on the SimpleDateFormat object to make it format your date in a certain timezone. So, to answer your question "how to create a date for a given(calendar_today's) time zone.": You don't and you can't, because a Date object has no timezone information. When you convert the Date object to a string, then you can specify the timezone to format the Date in.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: timezone information getting lost in java.sql.date
|
|
|