将国家代码 alpha-2 (IN) 转换为 alpha 3 (IND) 的 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3617160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java code to convert country codes alpha-2 (IN) to alpha 3 (IND)
提问by Mbg
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)?
使用 Java,是否有一种快速的方法可以将 alpha-2 国家/地区代码(IN 或 GB)转换为等效的 alpha-3 国家代码(IND 或 GBR)?
I can get the alpha-2 codes with:
我可以通过以下方式获得 alpha-2 代码:
String[] codes = java.util.Locale.getISOLanguages();
String[] 代码 = 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 .
这不是问题,实际上我的应用程序读取了 alpha-2 代码,但我需要输出 alpha-3 等效的 .
Is there a similar way like above to get the alpha-3 codes?
有没有类似上面的方法来获取 alpha-3 代码?
Any suggestions?
有什么建议?
采纳答案by Gopi
This works -
这有效 -
Locale locale = new Locale("en","IN");
System.out.println("Country=" + locale.getISO3Country());
Output:
输出:
Country=IND
回答by Arne Burmeister
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)
BTW:getISOLanguages()返回语言代码(小写),getISOCountries()返回国家代码(大写)
回答by stacker
Since you read in the codes, you can't hardcode them you rather should create a lookup table to convert into ISO codes.
由于您读入了代码,因此无法对其进行硬编码,您应该创建一个查找表以将其转换为 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" ) );
}
回答by Codelicious
Gopi's answer works. BUTtake 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
Gopi的回答有效。但请注意,返回的代码是 ISO 3166 国家/地区代码,而不是 ISO 4217 货币代码。这些略有不同,因此请谨慎使用

