I want to validate that 3 character ISO Country code coming in the request is valid or not using Java.
2 Digit Country code can be valid using this
But I want to validate for 3 character ISO code.
Java Version: 11
I want to validate that 3 character ISO Country code coming in the request is valid or not using Java.
2 Digit Country code can be valid using this
But I want to validate for 3 character ISO code.
Java Version: 11
Sample code to get country name based on 3 Letter code. Tweak and you can make your own validation method.
public static void main(String[] args) {
Map<String, String> countriesMap = buildISOCodeAndCountryMap();
String countryName = countriesMap.get("IND");
System.out.println(countryName);
}
public static Map<String, String> buildISOCodeAndCountryMap() {
Map<String, String> isoCodeAndCountryMap= new HashMap<String, String>();
String[] isoCountries = Locale.getISOCountries();
for (String country : isoCountries) {
Locale locale = new Locale("", country);
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
if (!"".equals(iso) && !"".equals(code)
&& !"".equals(name)) {
isoCodeAndCountryMap.put(iso, name);
}
}
return isoCodeAndCountryMap;
}