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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With