I ran into a strange issue. Here is a snippet of code that describes it:
DateTimeZone dtz = DateTimeZone.forOffsetHours(0);
DateTime dt = new DateTime(dtz);
System.out.println(dt);
System.out.println(dt.toDate());
the output is:
2012-02-29T17:24:39.055Z
Wed Feb 29 19:24:39 EET 2012
I’m located UTC+2, but this action is supposed to create a java.util.Date object which is initialized for UTC time. What am I missing?
Datedoesn’t know about a time zone at all – it only represents an instant in time (like Joda Time’sInstanttype). It’s just a number of milliseconds since the Unix epoch. When you callDate.toString(), it always uses the system local time zone for converting that into a readable text form.So there’s nothing wrong here – just an expectations failure over either the meaning of
java.util.Dateor itstoString()behaviour, or both.(As an aside, prefer
DateTimeZone.UTCover creating your own.)