GMT offsets: Java vs Unix

Java is interprets GMT[+-]<offset> time zone values differently than Unix utilities such as 'date'. For example, I have a simple test program that grabs the current time with a call to Calendar.getInstance() and then prints out this time using the DateFormat.getDateTimeInstance() formatter. Here is some output:

$ TZ=GMT-1 date

Thu May 10 17:26:39 GMT 2007

$ TZ=GMT+1 date

Thu May 10 15:26:43 GMT 2007

$ TZ=GMT-1 java -cp . TestCalendar

May 10, 2007 3:26:53 PM

$ TZ=GMT+1 java -cp . TestCalendar

May 10, 2007 5:26:58 PM

This was reproduced using a 1.4.2-13 and 1.5.0-07 JRE, both on Solaris 10 (i386), and with a 1.5.0-03 JRE on a Linux machine. And I have found documentation from Sun supporting that this is intended behavior for Java. Switching to UTC provides consistent results between Unix utilities and Java:

$ TZ=UTC-1 date

Thu May 10 17:37:22 UTC 2007

$ TZ=UTC-1 java -cp . TestCalendar

May 10, 2007 5:37:29 PM

What's with this inconsistency? Is there some reason for it? We are in the process of switching the value used for TZ for the environment in question, so no need to suggest that. I just want to understand this seemingly bizarre behavior.

Thanks in advance!

Chris Rorvick

[1316 byte] By [crorvicka] at [2007-11-27 4:00:26]
# 1

Found some old bug reports, the most relavent being:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4976879

Based on the submission dates of these bug reports, this doesn't appear to be a high priority. Java sometimes takes a "head in the sand" approach to some platform issues (e.g., completely dropping support for environment variables in 1.4.x JREs.) This seems like one of those cases.

If the string were being passed via a system property, then it would be arguable that Java can interpret it however it wants. But in this case the run-time environment is getting the value directly from the underlying system's environment. It clearly should provide a consistent interpretation of this value as the rest of the system (i.e., libc) and not its own.

crorvicka at 2007-7-12 9:05:03 > top of Java-index,Desktop,Runtime Environment...