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 com.ibm.icu.util.TimeZone class. Here is an example:
String[] timeZones = com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode);
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 = com.ibm.icu.util.TimeZone.getAvailableIDs(locale.getCountry());
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 = com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode);List<TimeZone> timeZoneList = new ArrayList<TimeZone>();for (String timeZone : timeZones){timeZoneList.add(TimeZone.getTimeZone(timeZone));}
No comments:
Post a Comment