Search This Blog

Thursday, July 23, 2009

Java Time and UTC

Java does have a date type that is truly time zone ignorant: long. It is the time in milliseconds since January 1, 1970 UTC. It is the value you get when you say System.currentTimeMillis. You can convert that for display, to local time, server time, UTC, or any other time zone you want, when you want to display it. java.util.Date and java.sql.Date and java.sql.Time all wrap long, and they contain no time zone information at all. If you call toString(), it will format the time into a string using the local time zone, but toString() is intended primarily as a diagnostic tool. Most other methods on those classes are deprecated because date/time localization is not a trivial issue: they don't really work. java.util.Calendar is currently the preferred way to deal with date/time localization in Java. Use java.text.DateFormat for more flexible capabilities (it uses java.util.Calendar internally to do its work).

No comments: