将国家/地区代码 alpha-2 (IN) 转换为 alpha 3 (IND) 的 Java 代码

发布于 2024-09-17 21:20:41 字数 305 浏览 2 评论 0原文

使用 Java,是否有一种快速方法来转换 alpha-2 国家/地区代码(IN 或 GB) 到 alpha-3 等效项(IND 或 GBR)?

我可以通过以下方式获取 alpha-2 代码:

String[] 代码 = java.util.Locale.getISOLanguages();

这不是问题,实际上我的应用程序读取 alpha-2 代码,但我需要输出 alpha-3 等效项。

有没有类似上面的方法来获取 alpha-3 代码?

有什么建议吗?

Using Java, Is there a quick way to convert an alpha-2 country code (IN or GB)
to the alpha-3 equivalent (IND or GBR)?

I can get the alpha-2 codes with:

String[] codes = java.util.Locale.getISOLanguages();

That's not a problem, actually my application reads in the alpha-2 code, but I need to output the alpha-3 equivalent .

Is there a similar way like above to get the alpha-3 codes?

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬 2024-09-24 21:20:41

这有效 -

    Locale locale = new Locale("en","IN");
    System.out.println("Country=" + locale.getISO3Country());

输出:

Country=IND

This works -

    Locale locale = new Locale("en","IN");
    System.out.println("Country=" + locale.getISO3Country());

Output:

Country=IND
若言繁花未落 2024-09-24 21:20:41

是的,简单地创建一个区域设置并从区域设置中获取:

String alpha3Country = new Locale("en", alpha2County).getISO3Country();

顺便说一句:getISOLanguages() 返回语言代码(小写),getISOCountries() 返回国家/地区代码(大写)

Yes, simple create a Locale and get if from the locale:

String alpha3Country = new Locale("en", alpha2County).getISO3Country();

BTW: getISOLanguages() returns language codes (lowercase), getISOCountries() return country codes (uppercase)

旧情勿念 2024-09-24 21:20:41

由于您读取了代码,因此无法对它们进行硬编码,您应该创建一个查找表来转换为 ISO 代码。

public static void main(String[] args) {
        // setup
        Locale[] availableLocales = Locale.getAvailableLocales();
        HashMap<String, String> map = new HashMap<String, String>();
        for ( Locale l : availableLocales ) {
            map.put( l.getCountry(), l.getISO3Country() );
        }
        // usage
        System.out.println( map.get( "IN" ) );
        System.out.println( map.get( "GB" ) );
    }

Since you read in the codes, you can't hardcode them you rather should create a lookup table to convert into ISO codes.

public static void main(String[] args) {
        // setup
        Locale[] availableLocales = Locale.getAvailableLocales();
        HashMap<String, String> map = new HashMap<String, String>();
        for ( Locale l : availableLocales ) {
            map.put( l.getCountry(), l.getISO3Country() );
        }
        // usage
        System.out.println( map.get( "IN" ) );
        System.out.println( map.get( "GB" ) );
    }
殤城〤 2024-09-24 21:20:41

Gopi 的答案有效。 但是请注意,返回的代码是 ISO 3166 国家/地区代码,而不是 ISO 4217 货币代码。这些略有不同,因此请谨慎使用

Gopi's answer works. BUT take note that the returned codes are the ISO 3166 country codes and not the ISO 4217 currency codes. These differ slightly so use with caution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文