• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

timezone information getting lost in java.sql.date

 
Sharadha Krishnamurthi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic