5

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.

emmby
  • 99,783
  • 65
  • 191
  • 249
  • Refer to this :http://stackoverflow.com/questions/1389837/is-there-a-way-to-get-a-timezone-with-only-a-country-code-valid-iso-3166-code – AllTooSir May 07 '13 at 17:32
  • @NoobUnChained - He's asking for TZID to country. Not country to TZID. This should be possible for anything except administrative zones. The other direction is what you are talking about, which of course wouldn't work. – Matt Johnson-Pint May 07 '13 at 20:05

3 Answers3

7

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;

}
noamdayan
  • 175
  • 3
  • 9
0

You cannot, there is no 1:1 relation from TimeZone to Country.
E.g there is also the timeZone "UTC"

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Don't see the problem to return null for UTC or similar. For absolute majority of timezones country code would be neat to have. – ruruskyi Apr 29 '14 at 10:11
0

The link you gave to zone.tab is actually the source of the data you're looking for. Of course, you'd want to use one that is authoritative, rather than the copy on Wikipedia.

The IANA/Olson database is implemented in Java with the JodaTime library. But I don't believe they expose zone.tab via any classes. At least I couldn't find it. Please update me if I am incorrect in this.

You can always import the file yourself and use it as needed.

UPDATE

I know nothing about Android, but I found something that might make sense to you. There appears to be a function that exposes the zone.tab data in libcore.icu.TimeZoneNames.forLocale(locale)

I searched, but I couldn't find any reference documentation on this.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • yeah, that's my problem. You would think it would be trivial to expose, but I don't see anywhere where it's exposed in the java/android apis (i hadn't looked at jodatime) – emmby May 07 '13 at 22:25
  • @emmby - See line # 149 of the link in my update. Does that help? – Matt Johnson-Pint May 07 '13 at 22:59