2

I have a long value "value" in which I hold the time in millis and if I change the timezone of my system from UTC +2:00 to UTC +13:00 new Date(value) return different results!

Can anyone tell me why and how to make it return the same date for the given value even if my timezone is different?

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
user641595
  • 61
  • 3
  • See http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java – sje397 Nov 07 '12 at 13:43
  • Its is returning the same date, but using different time zones. Like printing `1e3` or `1000.0` or `1000` The number is the same, only the format has changed. – Peter Lawrey Nov 07 '12 at 13:47

4 Answers4

4

No, the Date is not returning a different results. Date is just a wrapper around your time in millis, nothing more. It doesn't really understand time zones, hours, etc.

However! Date.toString() prints date using gregorian calendar with your current time zone. But even though the time zone is different, it still represents the same point in time.

Say I have this simple program:

new Date(1352296028515L).toString()

Normally it prints (I live in CET time zone):

Wed Nov 07 14:47:08 CET 2012

But I can override the time zone (-Duser.timezone=EST) or run the program in New York to get e.g.:

Wed Nov 07 08:47:08 EST 2012

However! Note that both dates actually represents the same hour, 13:47 UTC.

Simply put: calm down, everything is fine, it's just yet another reason to stay away from java.util.Date.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

Class java.util.Date does not know anything about timezones by itself. It's a container for a number of milliseconds since 01-01-1970, 00:00:00 GMT.

If you print a Date object by simply calling toString() on it (explicitly or implicitly), the date will be formatted to the timezone that your machine is set to. If you set your machine to another timezone, the printed date will look different - but it still refers to the same moment in time.

If you want to print the value of a Date object in a specific time zone, use a SimpleDateFormat object and set the timezone on that object:

Date date = ...;

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

// Show the date in the UTC timezone
df.setTimeZone(TimeZone.getTimeZone("UTC"));

System.out.println(df.format(date));
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

That is how time zones work. For instance it is 8:46 eastern and in California it is 5:46. The way to deal with time zones in this manner is to make everything based off of a central server, I recommend the server uses GMT. Then you take a preference from the user as to what their "true" time zone is.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
0

Do yourself a huge favour and avoid using the Java Calendar / Date classes. I strongly recommend using Joda Time - it's well thought out and bug-free.

By the way, there's nothing wrong with your code - it's just printing the result differently depending on the current TimeZone.

ct_
  • 2,269
  • 1
  • 16
  • 28