有没有人知道免费的java1.5包,它以枚举或EnumMap的形式提供了ISO3166-1国家代码列表?具体来说,我需要"ISO 3166-1-alpha-2代码元素“,即2个字符的国家代码,如"us”、"uk“、"de”等。创建一个非常简单(虽然繁琐),但如果apache或类似的地方已经有了一个标准的代码元素,就会节省一点时间。
发布于 2010-09-24 04:11:48
下面是我如何生成一个国家代码+国家名称的枚举:
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);
}
}
发布于 2013-10-17 21:36:35
如果你已经打算依赖Java locale,那么我建议你使用一个简单的HashMap,而不是为国家等创建新的类。
如果我只依赖Java本地化,我将如何使用它:
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());
}
填充地图后,无论何时需要,都可以从国家/地区名称中获取ISO代码。或者您也可以将其设置为ISO代码到国家名称的映射,只需相应地修改'put‘方法即可。
发布于 2010-02-20 02:01:12
有一种简单的方法可以使用语言名称生成此枚举。执行以下代码以生成要粘贴的枚举字段列表:
/**
* 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) + "\"),");
}
}
https://stackoverflow.com/questions/139867
复制相似问题