Jan 08

The ICU4J package provides a method for returning the available timezones for a given country code.

The following code will return a Map containing all available TimeZone’s for each country, keyed on country code.

public static Map<String, Set<TimeZone>> getAvailableTimeZones()
{
	Map<String, Set<TimeZone>> availableTimezones = 
		new HashMap<String, Set<TimeZone>>();
	
	// Loop through all available locales
	
	for (Locale locale : Locale.getAvailableLocales())
	{
		final String countryCode = locale.getCountry();
		
		// Locate the timezones added for this country so far
		// (This can be moved to inside the loop if depending
		// on whether country with no available timezones should
		// be in the result map with an empty set, 
		// or not included at all)
		
		Set<TimeZone> timezones = availableTimezones.get(countryCode);
		if (timezones==null)
		{
			timezones = new HashSet<TimeZone>();
			availableTimezones.put(countryCode, timezones);
		}

		// Find all timezones for that country (code) using ICU4J
		
		for (String id : 
			com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode))
		{
			// Add timezone to result map
			
			timezones.add(TimeZone.getTimeZone(id));
		}
	}
	return availableTimezones;
}

written by objects \\ tags: , ,


2 Responses to “Can I find all available timezones for a country?”

  1. peter Says:

    THANK YOU A LOT!!!

  2. worldclock Says:

    Can I find all available timezones for a country? | web development helpdesk – just great!

Leave a Reply

You must be logged in to post a comment.