Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get country for a given java TimeZone

I have a java TimeZone object, for example America/Denver. In theory, the IANA database lists one country code for that TimeZone, namely US.

In java or Android, how can I get the country for a specified TimeZone object?

UPDATE: Note that I'm not talking about mapping GMT+0700 to a specific country. Obviously, there might be multiple countries that map to a single raw offset. I'm talking about mapping a specific timezone code from https://en.wikipedia.org/wiki/Zone.tab to its associated singular country code.

like image 952
emmby Avatar asked May 07 '13 17:05

emmby


1 Answers

In order to map between countries and timezones I used the ICU library. Using the com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode) method I managed to map timezones to country codes.

public static Map<String, String> mapTimezonesToCountries() {
    Map<String, String> timezoneToCountry = new HashMap<>();

    String[] locales = Locale.getISOCountries();

    for (String countryCode : locales) {
        for (String id : com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode))
        {
            // Add timezone to result map

            timezoneToCountry.put(id, countryCode);
        }

    }
    return timezoneToCountry;

}
like image 121
noamdayan Avatar answered Sep 21 '22 11:09

noamdayan