Reputation: 100482
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.
Upvotes: 5
Views: 5243
Reputation: 173
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;
}
Upvotes: 7
Reputation: 241959
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.
Upvotes: 0
Reputation: 28767
You cannot, there is no 1:1 relation from TimeZone to Country.
E.g there is also the timeZone "UTC"
Upvotes: 0