开发者

Java code to convert country codes alpha-2 (IN) to alpha 3 (IND)

Using Java, Is there a quick way to convert an alpha-2 co开发者_如何转开发untry 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?


This works -

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

Output:

Country=IND


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)


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" ) );
    }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜