• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

timezone information getting lost in java.sql.date

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic