Showing posts with label timezone. Show all posts
Showing posts with label timezone. Show all posts

Sunday, August 31, 2008

How to get the server's timezone

The problem

How to get the server's timezone display name correctly and display it to the user in his locale.

server's default timezone

Firstly, you need to get the server's default timezone. This is easily achieved by the following call

final TimeZone timeZone = TimeZone.getDefault();

use daylight time?

Secondly, you need to find out if this timezone uses daylight time. This is important as some timezones change their names during daylight saving. Don't use the TimeZone.useDaylightTime() method

//final boolean daylight = timeZone.useDaylightTime();

This method works fine only for systems where the timezone never changes. If the administrator changes the time on the server, this change won't be reflected in subsequent calls. What you need to do instead, is to find out is the daylight savings is on right now.

final boolean daylight = timeZone.inDaylightTime(new Date());

user's locale

The last important step is to get the right locale, the locale you want this timezone name to display in. You could call Locale.getDefault(), but this returns server's default locale. You want the locale that the user is using. In a web application, the user's locale can be obtained from getLocale() method of ServletRequest object.

final Locale locale = servletRequest.getLocale();

final step - timezone display name

Now we can call getDisplayName method with all required parameters.

return timeZone.getDisplayName(daylight, TimeZone.LONG, locale);

Solution

To put this all together, the final solution may look like this method

private String getServerTimeZoneDisplayName()
{
final TimeZone timeZone = TimeZone.getDefault();
final boolean daylight = timeZone.inDaylightTime(new Date());
final Locale locale = servletRequest.getLocale();
return timeZone.getDisplayName(daylight, TimeZone.LONG, locale);
}

References

Monday, March 27, 2006

Commonwealth Games

Now as the Commonwealth Games 2006 in Melbourne, Australia are almost at the end I want to talk about what did these games bring to us in Java world.

In one word - confusion. The reason for that is two mistakes.

Let me explain. Australia as many other countries has summer time when we adjust our clocks and watches one hour ahead. For example, Sydney's time zone is 10 hours ahead of GMT (GMT+1000) and during summer time, the clock is one hour ahead, making the time zone 11 hours ahead of GMT (GMT+1100).

I followed the discussions on Australian Java Users' Group on this topic and I came to the following conclusion. The first mistake was made by Aussie goverment. They should not be allowed to change the time as they will. Who cares about... whatever. The second came soon after. Microsoft released a patch that was supposed to fix this exceptional case. Their release was a new timezone - second mistake. Yes, new timezone. And as a result all Java applications were confused as they did not know how to deal with new timezone they had no idea about. So they all defaulted to GMT.

Don't worry the fix is out there already. Sun released new updates to JDKs and JREs and advises the affected users to update them with the latest releases.

For those of you who are still using J2SE 1.3.1, I hope it is not too late as the new release is available only after 22 March.

Read more on Australian Time Zone Changes Affect Java Applications.


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.