File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes timezone information getting lost in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "timezone information getting lost in" Watch "timezone information getting lost in" New topic

timezone information getting lost in

Sharadha Krishnamurthi

Joined: Jun 30, 2005
Posts: 1

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 out of it,using the code above, date is created in the current time to create a date for a given(calendar_today's) time zone.


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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

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 8 API documentation
I agree. Here's the link:
subject: timezone information getting lost in
It's not a secret anymore!