找国给定的Java时区 [英] Get country for a given java TimeZone

查看:338
本文介绍了找国给定的Java时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆