Yoav Schwartz
Yoav Schwartz

Reputation: 708

custom and dynamic time zones in java

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?

Upvotes: 1

Views: 3571

Answers (4)

ZZ Coder
ZZ Coder

Reputation: 75456

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718826

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.

Upvotes: 3

skaffman
skaffman

Reputation: 403481

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

Upvotes: 0

Jesper
Jesper

Reputation: 206816

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.

Upvotes: 0

Related Questions