Is there an open source java enum of ISO 3166-1 country codes
Asked Answered
S

15

97

Does anyone know of a freely available java 1.5 package that provides a list of ISO 3166-1 country codes as a enum or EnumMap? Specifically I need the "ISO 3166-1-alpha-2 code elements", i.e. the 2 character country code like "us", "uk", "de", etc. Creating one is simple enough (although tedious), but if there's a standard one already out there in apache land or the like it would save a little time.

Sundae answered 26/9, 2008 at 14:26 Comment(2)
Note that ISO 3166-1-alpha-2 for Great Britain is GB not UK as in your question.Turbosupercharger
@Ken yes you can but then you have to maintain that list and keep it in sync with wikipedia. The problem grows...Instanter
L
126

Now an implementation of country code (ISO 3166-1 alpha-2/alpha-3/numeric) list as Java enum is available at GitHub under Apache License version 2.0.

Example:

CountryCode cc = CountryCode.getByCode("JP");

System.out.println("Country name = " + cc.getName());                // "Japan"
System.out.println("ISO 3166-1 alpha-2 code = " + cc.getAlpha2());   // "JP"
System.out.println("ISO 3166-1 alpha-3 code = " + cc.getAlpha3());   // "JPN"
System.out.println("ISO 3166-1 numeric code = " + cc.getNumeric());  // 392

Last Edit 2016-Jun-09

CountryCode enum was packaged into com.neovisionaries.i18n with other Java enums, LanguageCode (ISO 639-1), LanguageAlpha3Code (ISO 639-2), LocaleCode, ScriptCode (ISO 15924) and CurrencyCode (ISO 4217) and registered into the Maven Central Repository.

Maven

<dependency>
  <groupId>com.neovisionaries</groupId>
  <artifactId>nv-i18n</artifactId>
  <version>1.29</version>
</dependency>

Gradle

dependencies {
  compile 'com.neovisionaries:nv-i18n:1.29'
}

GitHub

https://github.com/TakahikoKawasaki/nv-i18n

Javadoc

https://takahikokawasaki.github.io/nv-i18n/

OSGi

Bundle-SymbolicName: com.neovisionaries.i18n
Export-Package: com.neovisionaries.i18n;version="1.28.0"
Lobscouse answered 18/6, 2012 at 14:0 Comment(3)
Hi Takahiko thanks for that! I just added three missing countries, sorry I was in a hurry and I just forked the repo, you'll find the updates here: github.com/bbossola/CountryCodeYehudit
Hi Bruno! Thank you for finding the missing entries (AL, AN and GN). I referred to your forked repository and manually copied them to my repository. Thank you!Lobscouse
In case you are doing banking stuff, there is an Apache-2-licensed library called "iban4j" (namespace org.iban4j) to handle IBAN and BIC codes and it also has a CountryCode enum: CountryCodeTeutonism
S
60

This code gets 242 countries in Sun Java 6:

String[] countryCodes = Locale.getISOCountries();

Though the ISO website claims there are 249 ISO 3166-1-alpha-2 code elements, though the javadoc links to the same information.

Soul answered 26/9, 2008 at 15:30 Comment(7)
This information is hardcoded. You would need to update JRE regulary to keep updated :)Sphenic
In Java 7 there are 247 countries, still less than in the official standard (which is 249).Susi
The ones missing are: SOUTH SUDAN (SS) and SINT MAARTEN (DUTCH PART) (SX)Susi
Java 1.6.0_33-b05 includes 248, only missing out SS now. This is simply because SS is the most recent (2011) country to be added and Java 6 source has not been updated.Hapsburg
The OP is asking for "a freely available java 1.5 package": how an answer requesting Java6 or Java7 applies to that? Downvoted.Yehudit
On java version: 1.8.0_152 there are 250 country codes availableOrthogenic
Note that if you are planning on using the display names of this library, they are not all up to ISO3166 standard: bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8245072Warplane
D
11

Here's how I generated an enum with country code + country name:

package countryenum;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class CountryEnumGenerator {
    public static void main(String[] args) {
        String[] countryCodes = Locale.getISOCountries();
        List<Country> list = new ArrayList<Country>(countryCodes.length);

        for (String cc : countryCodes) {
            list.add(new Country(cc.toUpperCase(), new Locale("", cc).getDisplayCountry()));
        }

        Collections.sort(list);

        for (Country c : list) {
            System.out.println("/**" + c.getName() + "*/");
            System.out.println(c.getCode() + "(\"" + c.getName() + "\"),");
        }

    }
}

class Country implements Comparable<Country> {
    private String code;
    private String name;

    public Country(String code, String name) {
        super();
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    @Override
    public int compareTo(Country o) {
        return this.name.compareTo(o.name);
    }
}
Dawdle answered 23/9, 2010 at 20:11 Comment(4)
added one line in print to enable intellitxtBruyn
@David it generates the body of an enumDawdle
Nice generation code, but you need to remove the special characters. For example CÔTE_D'IVOIRE cannot be used as an enum :P.Brunhild
Note that the names of this library are not up to ISO3166 standard: bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8245072Warplane
F
11

If you are already going to rely on Java locale, then I suggest using a simple HashMap instead of creating new classes for countries etc.

Here's how I would use it if I were to rely on the Java Localization only:

private HashMap<String, String> countries = new HashMap<String, String>();
String[] countryCodes = Locale.getISOCountries();

for (String cc : countryCodes) {
    // country name , country code map
    countries.put(new Locale("", cc).getDisplayCountry(), cc.toUpperCase());
}

After you fill the map, you can get the ISO code from the country name whenever you need it. Or you can make it a ISO code to Country name map as well, just modify the 'put' method accordingly.

Fibrovascular answered 17/10, 2013 at 13:36 Comment(1)
Note that the display countries are not all up to the ISO3166 standard: bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8245072Warplane
Q
3

There is an easy way to generate this enum with the language name. Execute this code to generate the list of enum fields to paste :

 /**
  * This is the code used to generate the enum content
  */
 public static void main(String[] args) {
  String[] codes = java.util.Locale.getISOLanguages();
  for (String isoCode: codes) {
   Locale locale = new Locale(isoCode);
   System.out.println(isoCode.toUpperCase() + "(\"" + locale.getDisplayLanguage(locale) + "\"),");
  }
 }
Quinquefid answered 19/2, 2010 at 18:1 Comment(1)
This gives you a list of languages, not countries.Jaquiss
K
3

If anyone is already using the Amazon AWS SDK it includes com.amazonaws.services.route53domains.model.CountryCode. I know this is not ideal but it's an alternative if you already use the AWS SDK. For most cases I would use Takahiko's nv-i18n since, as he mentions, it implements ISO 3166-1.

Krug answered 28/10, 2015 at 14:57 Comment(0)
C
2

AWS Java SDK has CountryCode.

Cancellate answered 3/3, 2020 at 18:25 Comment(0)
H
1

Not a java enum, but a JSON version of this is available at http://country.io/names.json

Headlight answered 5/9, 2014 at 2:1 Comment(0)
O
1

There is standard java.util.Locale.IsoCountryCode since Java 9.

It uses the data from java/util/LocaleISOData.java.

Onomatology answered 21/3, 2022 at 15:25 Comment(2)
Does it include countries like US and DE?Keystroke
@Keystroke , yes: github.com/openjdk/jdk/blob/master/src/java.base/share/classes/….Onomatology
G
1

JCountry is an open source library containing the ISO 3166-1 codes, and it's corresponding country name translations to different languages. It is a wrapper for the debian iso-codes info. It also contains the iso codes for languages (iso 639-2) and the corresponding language name translations.

// Maven
<dependency>
    <groupId>io.github.castmart</groupId>
    <artifactId>jcountry</artifactId>
    <version>0.0.2</version>
</dependency>

Here is the repo: https://github.com/castmart/jcountry

JCountry jcountry = JCountry.getInstance();
CountryDB countryDB = jcountry.getCountriesDB();
// Get the countries DB hash maps <String, Country>
HashMap<String, Country> dbByAlpha2 = countryDB.getCountriesMapByAlpha2();
HashMap<String, Country> dbByAlpha3 = countryDB.getCountriesMapByAlpha3();
HashMap<String, Country> dbByName = countryDB.getCountriesMapByName();

// Get Translations by language based locale 
Optional<ResourceBundle> bundle = countryDB.getCountriesTranslations(Locale.GERMAN);

// MX -> Mexiko
String translatedCountryName = bundle.get().getString(dbByAlpha2.get("MX").getName());

// Languages DB
LanguageDB languageDB = new LanguageDBImpl(true);
HashMap<String, Language> dbByAlpha2 = languageDB.getLanguagesMapByAlpha2();

// Get Translations by language based locale 
Optional<ResourceBundle> bundle = languageDB.getLanguagesTranslations(Locale.GERMAN);

// Spanisch (Kastilisch)
String translatedCountryName = bundle.get().getString(dbByAlpha2.get("es").getName());
Greensboro answered 7/7, 2023 at 22:4 Comment(2)
What type do vars resolve to? It's not very obvious from the context. Especially, for people reading this who don't already know the libraryKeystroke
Good point, I already updated itGreensboro
P
0

This still does not answer the question. I was also looking for a kind of enumerator for this, and did not find anything. Some examples using hashtable here, but represent the same as the built-in get

I would go for a different approach. So I created a script in python to automatically generate the list in Java:

#!/usr/bin/python
f = open("data.txt", 'r')
data = []
cc = {}

for l in f:
    t = l.split('\t')
    cc = { 'code': str(t[0]).strip(), 
           'name': str(t[1]).strip()
    }
    data.append(cc)
f.close()

for c in data:
    print """
/**
 * Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a> 
 * for <b><i>%(name)s</i></b>.
 * <p>
 * This constant holds the value of <b>{@value}</b>.
 *
 * @since 1.0
 *
 */
 public static final String %(code)s = \"%(code)s\";""" % c

where the data.txt file is a simple copy&paste from Wikipedia table (just remove all extra lines, making sure you have a country code and country name per line).

Then just place this into your static class:

/**
 * Holds <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a>
 * constant values for all countries. 
 * 
 * @since 1.0
 * 
 * </p>
 */
public class CountryCode {

    /**
     * Constructor defined as <code>private</code> purposefully to ensure this 
     * class is only used to access its static properties and/or methods.  
     */
    private CountryCode() { }

    /**
     * Defines the <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO_3166-1_alpha-2</a> 
     * for <b><i>Andorra</i></b>.
     * <p>
     * This constant holds the value of <b>{@value}</b>.
     *
     * @since 1.0
     *
     */
     public static final String AD = "AD";

         //
         // and the list goes on! ...
         //
}
Philipson answered 25/11, 2011 at 3:41 Comment(0)
R
0

I didn't know about this question till I had just recently open-sourced my Java enum for exactly this purpose! Amazing coincidence!

I put the whole source code on my blog with BSD caluse 3 license so I don't think anyone would have any beefs about it.

Can be found here. https://subversivebytes.wordpress.com/2013/10/07/java-iso-3166-java-enum/

Hope it is useful and eases development pains.

Residential answered 8/10, 2013 at 7:56 Comment(0)
I
0

I have created an enum, which you address by the english country name. See country-util.
On each enum you can call getLocale() to get the Java Locale.

From the Locale you can get all the information you are used to, fx the ISO-3166-1 two letter country code.

public enum Country{

    ANDORRA(new Locale("AD")),
    AFGHANISTAN(new Locale("AF")),
    ANTIGUA_AND_BARBUDA(new Locale("AG")),
    ANGUILLA(new Locale("AI")),
    //etc
    ZAMBIA(new Locale("ZM")),
    ZIMBABWE(new Locale("ZW"));

    private Locale locale;

    private Country(Locale locale){
        this.locale = locale;
    }

    public Locale getLocale(){
        return locale;
    }

Pro:

  • Light weight
  • Maps to Java Locales
  • Addressable by full country name
  • Enum values are not hardcoded, but generated by a call to Locale.getISOCountries(). That is: Simply recompile the project against the newest java version to get any changes made to the list of countries reflected in the enum.

Con:

  • Not in Maven repository
  • Most likely simpler / less expressive than the other solutions, which I don't know.
  • Created for my own needs / not as such maintained. - You should probably clone the repo.
Inverson answered 20/9, 2017 at 10:31 Comment(0)
E
0

I found the IsoCountry list here, it has 2 and 3 char country codes

Euphemism answered 13/7, 2022 at 9:1 Comment(0)
I
0

To obtain the current device locale in Alpha-3 ISO (XXX) format:

fun getCurrentCountryCode(): String? {
    val tm = context.getSystemService(AppCompatActivity.TELEPHONY_SERVICE) as TelephonyManager
    val countryCodeValue = tm.networkCountryIso

    val locale: Locale? = Locale.getAvailableLocales().firstOrNull {
        it.country.lowercase() == countryCodeValue.lowercase()
    }

    return locale?.isO3Country
}

To obtain the current device language locale (xx-XX) format:

fun getCurrentLocale(): String? {
    return try {
        val locale = context.resources.configuration.locales[0]
        return "${locale.language}-${locale.country}"
    } catch (e: Exception) {
        e.printStackTrace()
        null
    }
}
Itu answered 22/2, 2023 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.