tags:

views:

215

answers:

4

I have a JSE 6 application that runs on an OS with a custom timezone.

TimeZone.getDefaultTimeZone() returns 'GMT' as the timezone altough this is not correct. Does java supports custom time zones?

Another problem is that the time zone configuration (daylight saving time transitions) could change while the application is running.

Will TimeZone.getDefaultTimeZone() return the updated version of the timezone at every call?

+1  A: 

There is a TimeZone.setDefaultTimeZone() that you can use to change the timezone. However, you need to do this at the right point(s) in your application since the default timezone is stored in an inheritable thread local. (This is not documented in the Javadocs, by the way!)

I don't believe that Java will refresh the timezone information (e.g. update the rules) while the JVM is running. Certainly it cannot detect that the system's default timezone has changed, since (on Unix/Linux) the default timezone is communicated via environment variable settings, and a process cannot see changes to its parent's environment variables.

Stephen C
A: 

Not a full answer, but...:

Will TimeZone.getDefaultTimeZone() return the updated version of the timezone at every call?

You could easily test this yourself; write a small program that prints the result of TimeZone.getDefaultTimeZone() every few seconds or so, then change the timezone setting of the OS while the program is running and see if the output changes.

Jesper
A: 

The Javadoc for java.util.TimeZone describes how to configure custom zones.

skaffman
A: 

Java only understand very simple custom zone format like,

  GMT+9:30

It will default to GMT if you have anything it doesn't recognize.

Java's default zone doesn't change when OS timezone changes. You have to restart JVM. It used to refresh timezone on every call to getDefaultTimeZone() but I don't know why it stopped doing that since Java 5.

Another gotcha with Java's Timezone is that setDefaultTimezone() only affects the calling thread if running with SecurityManager.

ZZ Coder