找国给定的Java时区 [英] Get country for a given java TimeZone
问题描述
我有一个Java TimeZone对象,例如美洲/丹佛
。从理论上讲, IANA数据库列出一个国家code表示的时区,即美
。
I have a java TimeZone object, for example America/Denver
. In theory, the IANA database lists one country code for that TimeZone, namely US
.
在Java的还是Android,如何才能得到该国指定的TimeZone对象?
In java or Android, how can I get the country for a specified TimeZone object?
的更新:请注意,我不是在谈论映射 GMT + 0700
到一个特定的国家。显然,可能有映射到一个单一原始偏移量的多个国家。我说的是从 https://en.wikipedia.org映射一个特定的时区code /wiki/Zone.tab 来其关联的单一国家code。的
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.
推荐答案
在为国家和时区之间进行映射我用了ICU的库。
使用com.ibm.icu.util.TimeZone.getAvailableIDs(国家code)的方法,我设法映射时区为国家codeS。
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;
}
这篇关于找国给定的Java时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!