This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes java.util.Date on different JVMs on different timezones Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.util.Date on different JVMs on different timezones" Watch "java.util.Date on different JVMs on different timezones" New topic
Author

java.util.Date on different JVMs on different timezones

Jimmy Ho
Ranch Hand

Joined: Jul 31, 2007
Posts: 61
If I serialize a java.util.Date object, and send it to another JVM that's on a different time zone, will the time zones be automatically accounted for?

I'm assuming it does because in the JavaDocs, it says the Date object really just stores the milliseconds since 1/1/1970, 00:00:00 **GMT**. Theoretically, this means if I serialize a Date object that's 3pm Eastern Time, and send it to a JVM that's on Pacific Time, then printing out the date should show either 3pm Eastern Time, or 12pm Pacific Time, BUT NOT 3pm Pacific Time.

So that's the behavior I want, and it seems to behave that way according to the documentation and my understanding of Java. Does anyone see anything wrong with this?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Class java.util.Date doesn't know anything about timezones. As you already found out, a Date object indicates a specific point in time, independent of time timezone. So if you serialize a Date object, send it to a remote computer in a different timezone, and deserialize it there, the deserialized Date object still indicates the exact same point in time.

So indeed, if you have a Date that represents 09/25/2007 3pm Eastern time, it will still indicate 3pm Eastern time, which corresponds to 12pm Pacific time, if you send it to a computer in the Pacific timezone.

If you print a Date object, it will by default be displayed in the timezone of the computer you're running your Java code on. If you want the Date object printed in a specific timezone, then use a DateFormat object and set the timezone on that format object:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.Date on different JVMs on different timezones
 
Similar Threads
java.util.Date on different JVMs on different timezones
Query about lastModified()
TimeZone Conversion
Problem with a date/time value
please help with an example