Thursday, September 11, 2008

Sun JDK + Linux = Timezone Issues

At work, we have three primary JBoss application servers. On two of them, Java was reporting the right timezone. However, one of them was not. Everything about the machines is nearly identical so I couldn't figure out why until I found this comment in a bug report which talks about an old issue with the JDK.

Here is the test we were using...
import org.joda.time.DateTime;
public class TestDate {
public static void main(String[] args) {
System.out.println("Current date is: " + new DateTime().toString());
}
}

and the results:
app2: Current date is: 2008-09-11T19:13:19.650Z
app3: Current date is: 2008-09-11T12:13:40.493-07:00

There are several solutions to the problem. Each one has drawbacks. I choose to go with forcing the TZ environment variable when starting up the JVM cause it was the easiest solution.

export TZ=`cat /etc/timezone`

No comments: