I’m looking for a better way to set Java’s default timezone. We’re currently hitting our DB2 database to get it’s current timezone, but it’s not a timezone name, just the current hours/minutes offset value. So we take that value and construct an ID like “GMT-07:00”.
That seems to cause two problems:
- We have to make sure to reset for DST start/end. e.g. we have set up a scheduled task the queries the database and sets the timezone. Annoying.
- For reasons unknown to us (yet), our scheduled task will SOMETIMES set the wrong timezone.
Assuming that DB2 always returns the correct value then I can only imagine that “GMT-07:00” is vague and between Java/it’s timezone db/the OS, it plucks a different value from the table.
After computing a string like “GMT-07:00” our code does this:
String timezoneGMTOffsetString = "GMT-07:00";
TimeZone localTimeZone = TimeZone.getTimeZone(timezoneGMTOffsetString);
TimeZone.setDefault(localTimeZone);
System.setProperty("user.timezone", timezoneGMTOffsetString);
We think we’re just going to have to set the value externally and use -Duser.timezone="America/Los_Angeles" when starting Tomcat. And then we can scrap the scheduled job!
Any advice? Java 6.0_26
The preferred solution is always covert to UTC and from UTC. Use joda-time library for conversion.