18

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?

Gopi
  • 10,073
  • 4
  • 31
  • 45
Mbg
  • 183
  • 1
  • 1
  • 4
  • This code can be used, for conversion vice versa from Alpha-3 into Alpha-2: `Map countryCodeMap = Stream.of(Locale.getAvailableLocales()) .filter(locale -> !locale.getISO3Country().isEmpty()) .collect(Collectors.toMap(Locale::getISO3Country, Locale::getCountry)); countryCodeMap.get("IND")` – Datz Jul 05 '23 at 10:31

4 Answers4

20

This works -

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

Output:

Country=IND
Gopi
  • 10,073
  • 4
  • 31
  • 45
  • This is great, as I can swop in a variable containing the new country code each time, and it will give me back the corresponding 3 char code for every country. Locale locale = new Locale("en",myCode); The language will all be english, but I'm not concerned about language. – Mbg Sep 01 '10 at 11:52
13

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)

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
  • This is great, as I can swop in a variable containing the new country code each time, and it will give me back the corresponding 3 char code for every country. The language will all be english, but I'm not concerned about language. – Mbg Sep 01 '10 at 11:53
  • On a side note, you can equivalently do `new Locale("", alpha2County).getISO3Country()`. Specifying the English language here is kind of irrelevant. – Jonik Feb 17 '15 at 13:40
  • Faulty BTW: Country codes are UPPERCASE, language codes lowercase. – JN01 Mar 07 '16 at 10:16
4

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" ) );
    }
stacker
  • 68,052
  • 28
  • 140
  • 210
1

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

Codelicious
  • 604
  • 10
  • 12
  • 2
    Please, try to read this http://stackoverflow.com/about, to get more understanding about questions/answers here on SO. Your contribution is not answering the question. It is more a comment, which you can add once you'll increase your reputation: http://stackoverflow.com/faq#reputation – Radim Köhler Feb 24 '14 at 12:40