Saturday, July 18, 2009

Getting all time zones of a Country in Java

In this post I wrote about how to get a complete time zone list in java. In terms of UI, it is not always the best thing to show a complete list of time zones. The list is quite long and the data is ordered by continent. Sometimes it makes it harder to find a time zone. A nice solution is allowing to filter time zones according to a selected country. Unfortunately, there is no built-in way to know the time zones of a given locale. But, there is a very nice open source project called ICU4J (International Components for Unicode) that allows very easily to get all time zones of a specific Locale. In order to that, we just have to call the getAvailableIDs method of the class. Here is an example:

String[] timeZones =;

This example will return all the available time zones in Israel. Note that the input to this function is country code. If we had a locale we could have written:

String[] timeZones =;

Note that the result of this function is array of time zone ids. This result can be easily converted to an array of time zones:

String[] timeZones =;
List<TimeZone> timeZoneList = new ArrayList<TimeZone>();
for (String timeZone : timeZones)

No comments:

Post a Comment