Apr 01

The TimeZone class has a getOffset() method that will return the offset for a specified date. For example to get the timezone offset for today you would use the following:

Date today = new Date();
TimeZone tz = TimeZone.getDefault();
long offset = tz.getOffset(today);

written by objects \\ tags: , ,

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 : 
			// Add timezone to result map
	return availableTimezones;

written by objects \\ tags: , ,