我想获得所有可能的货币。
Java 7提供了这样的功能。
public static Set<java.util.Currency> java.util.Currency.getAvailableCurrencies()
但是,我仍在使用Java 6进行开发和部署。我可以知道如何获得所有可能的货币吗?代码示例最受欢迎。
答案 0 :(得分:21)
在研究了ISO表和Currency类文档后,您似乎可以要求货币作为代码或Locale;并且Locale类具有getAvailableLocales()
方法。
所以,代码是:
public static Set<Currency> getAllCurrencies()
{
Set<Currency> toret = new HashSet<Currency>();
Locale[] locs = Locale.getAvailableLocales();
for(Locale loc : locs) {
try {
Currency currency = Currency.getInstance( loc );
if ( currency != null ) {
toret.add( currency );
}
} catch(Exception exc)
{
// Locale not found
}
}
return toret;
}
希望这有帮助。
答案 1 :(得分:9)
如果您想要所有货币,请将ISO 4217(xml)定义与您的要求和openJDK7中的示例相结合。如果迭代默认语言环境,则只能获得ISO 4217定义的180个中的78个。
String[] viaLocales = { "JPY", "CNY", "SDG", "RON", "MKD", "MXN", "CAD",
"ZAR", "AUD", "NOK", "ILS", "ISK", "SYP", "LYD", "UYU", "YER", "CSD",
"EEK", "THB", "IDR", "LBP", "AED", "BOB", "QAR", "BHD", "HNL", "HRK",
"COP", "ALL", "DKK", "MYR", "SEK", "RSD", "BGN", "DOP", "KRW", "LVL",
"VEF", "CZK", "TND", "KWD", "VND", "JOD", "NZD", "PAB", "CLP", "PEN",
"GBP", "DZD", "CHF", "RUB", "UAH", "ARS", "SAR", "EGP", "INR", "PYG",
"TWD", "TRY", "BAM", "OMR", "SGD", "MAD", "BYR", "NIO", "HKD", "LTL",
"SKK", "GTQ", "BRL", "EUR", "HUF", "IQD", "CRC", "PHP", "SVC", "PLN",
"USD" };
String[] iso4217WithoutViaLocales = { "XBB", "XBC", "XBD", "UGX", "MOP",
"SHP", "TTD", "UYI", "KGS", "DJF", "BTN", "XBA", "HTG", "BBD", "XAU",
"FKP", "MWK", "PGK", "XCD", "COU", "RWF", "NGN", "BSD", "XTS", "TMT",
"GEL", "VUV", "FJD", "MVR", "AZN", "MNT", "MGA", "WST", "KMF", "GNF",
"SBD", "BDT", "MMK", "TJS", "CVE", "MDL", "KES", "SRD", "LRD", "MUR",
"CDF", "BMD", "USN", "CUP", "USS", "GMD", "UZS", "CUC", "ZMK", "NPR",
"NAD", "LAK", "SZL", "XDR", "BND", "TZS", "MXV", "LSL", "KYD", "LKR",
"ANG", "PKR", "SLL", "SCR", "GHS", "ERN", "BOV", "GIP", "IRR", "XPT",
"BWP", "XFU", "CLF", "ETB", "STD", "XXX", "XPD", "AMD", "XPF", "JMD",
"MRO", "BIF", "CHW", "ZWL", "AWG", "MZN", "CHE", "XOF", "KZT", "BZD",
"XAG", "KHR", "XAF", "GYD", "AFN", "SOS", "TOP", "AOA", "KPW" };
答案 2 :(得分:7)
对于想要将其作为Java枚举
的人来说,这可能会有所帮助 public enum Currency {
AED("AED"), AFN("AFN"), ALL("ALL"), AMD("AMD"), ANG("ANG"), AOA("AOA"), ARS("ARS"), AUD("AUD"), AWG("AWG"), AZN("AZN"), BAM("BAM"), BBD("BBD"),
BDT("BDT"), BGN("BGN"), BHD("BHD"), BIF("BIF"), BMD("BMD"), BND("BND"), BOB("BOB"), BOV("BOV"), BRL("BRL"), BSD("BSD"), BTN("BTN"), BWP("BWP"),
BYN("BYN"), BYR("BYR"), BZD("BZD"), CAD("CAD"), CDF("CDF"), CHE("CHE"), CHF("CHF"), CHW("CHW"), CLF("CLF"), CLP("CLP"), CNY("CNY"), COP("COP"),
COU("COU"), CRC("CRC"), CUC("CUC"), CUP("CUP"), CVE("CVE"), CZK("CZK"), DJF("DJF"), DKK("DKK"), DOP("DOP"), DZD("DZD"), EGP("EGP"), ERN("ERN"),
ETB("ETB"), EUR("EUR"), FJD("FJD"), FKP("FKP"), GBP("GBP"), GEL("GEL"), GHS("GHS"), GIP("GIP"), GMD("GMD"), GNF("GNF"), GTQ("GTQ"), GYD("GYD"),
HKD("HKD"), HNL("HNL"), HRK("HRK"), HTG("HTG"), HUF("HUF"), IDR("IDR"), ILS("ILS"), INR("INR"), IQD("IQD"), IRR("IRR"), ISK("ISK"), JMD("JMD"),
JOD("JOD"), JPY("JPY"), KES("KES"), KGS("KGS"), KHR("KHR"), KMF("KMF"), KPW("KPW"), KRW("KRW"), KWD("KWD"), KYD("KYD"), KZT("KZT"), LAK("LAK"),
LBP("LBP"), LKR("LKR"), LRD("LRD"), LSL("LSL"), LYD("LYD"), MAD("MAD"), MDL("MDL"), MGA("MGA"), MKD("MKD"), MMK("MMK"), MNT("MNT"), MOP("MOP"),
MRO("MRO"), MUR("MUR"), MVR("MVR"), MWK("MWK"), MXN("MXN"), MXV("MXV"), MYR("MYR"), MZN("MZN"), NAD("NAD"), NGN("NGN"), NIO("NIO"), NOK("NOK"),
NPR("NPR"), NZD("NZD"), OMR("OMR"), PAB("PAB"), PEN("PEN"), PGK("PGK"), PHP("PHP"), PKR("PKR"), PLN("PLN"), PYG("PYG"), QAR("QAR"), RON("RON"),
RSD("RSD"), RUB("RUB"), RWF("RWF"), SAR("SAR"), SBD("SBD"), SCR("SCR"), SDG("SDG"), SEK("SEK"), SGD("SGD"), SHP("SHP"), SLL("SLL"), SOS("SOS"),
SRD("SRD"), SSP("SSP"), STD("STD"), SYP("SYP"), SZL("SZL"), THB("THB"), TJS("TJS"), TMT("TMT"), TND("TND"), TOP("TOP"), TRY("TRY"), TTD("TTD"),
TWD("TWD"), TZS("TZS"), UAH("UAH"), UGX("UGX"), USD("USD"), USN("USN"), UYI("UYI"), UYU("UYU"), UZS("UZS"), VEF("VEF"), VND("VND"), VUV("VUV"),
WST("WST"), XAF("XAF"), XAG("XAG"), XAU("XAU"), XBA("XBA"), XBB("XBB"), XBC("XBC"), XBD("XBD"), XCD("XCD"), XDR("XDR"), XFU("XFU"), XOF("XOF"),
XPD("XPD"), XPF("XPF"), XPT("XPT"), XSU("XSU"), XTS("XTS"), XUA("XUA"), XXX("XXX"), YER("YER"), ZAR("ZAR"), ZMW("ZMW");
private String iso4217Code = "";
Currency(String code) {
this.iso4217Code = code;
}
@Override
public String toString() {
return iso4217Code;
}
}
答案 3 :(得分:3)
否则版本稍微好一点Baltasarq代码以避免重复:
ArrayList<String> currencys = new ArrayList<String>();
Locale[] locs = Locale.getAvailableLocales();
for(Locale loc : locs) {
try {
String val=Currency.getInstance(loc).getCurrencyCode();
if(!currencys.contains(val))
currencys.add(val);
} catch(Exception exc)
{
// Locale not found
}
Collections.sort(currencys);
}
我在android上测试过。
答案 4 :(得分:1)
我一直在使用Baltasarq的答案,我发现它因为抛出了一些java.lang.IllegalArgumentException而减慢了我的应用程序,这不是他的错,但实际上是货币库的作者,他已经选择了抛出异常而不是错误(Raw Code)。
然而,在查看该库时,有一个公共静态方法getAvailableCurrencies
,它将为您提供所有可用的货币作为一个集合,而无需遍历所有可用的语言环境并抛出一堆异常。
修改后的代码现在看起来像这样:
public static Set<Currency> getAllCurrencies() {
Set<Currency> currencies = Currency.getAvailableCurrencies();
return currencies;
}
当然,这很愚蠢,您也可以使用Currency自己的静态方法并为自己节省不必要的方法调用。 #SaveTheStack
答案 5 :(得分:0)
使用JodaMoney。在joda-money.jar里面我发现MoneyData.csv它拥有所有的ISO 4217货币。使用 org.joda.money.CurrencyUnit 来提取货币。
`
public static void main(String[] args) {
List<CurrencyUnit> currencyUnits = CurrencyUnit.registeredCurrencies();
for (CurrencyUnit currencyUnit : currencyUnits) {
System.out.println(
currencyUnit.getCode() +" " +
currencyUnit.getNumericCode() + " "+
currencyUnit.getNumeric3Code()+ " "+
currencyUnit.getCountryCodes());
}
`
答案 6 :(得分:0)
我正在使用Currency java类,但我无法找到阿富汗货币(AFN),因此我不确定是否可以依赖此Currency Library
。要解决此问题,我使用的是joda-money
库。
所以我把pom.xml
这个:
<dependency>
<groupId>org.joda</groupId>
<artifactId>joda-money</artifactId>
<version>0.11</version>
</dependency>
在我的代码中我使用了这个:
List<CurrencyUnit> currencyUnits = CurrencyUnit.registeredCurrencies();
获得所有货币,AFN就在那里,所以我现在正在使用这个库。
我希望它可以提供帮助。
答案 7 :(得分:0)
您还可以使用maven上提供的库nv-i18n
:
<dependency>
<groupId>com.neovisionaries</groupId>
<artifactId>nv-i18n</artifactId>
<version>1.22</version>
</dependency>
要查看所有货币代码,您只需使用:
for (CurrencyCode code : CurrencyCode.values()) {
System.out.format("[%s] %03d %s\n", code, code.getNumeric(), code.getName());
}
您可以在此处查看有关此类和库的更多详细信息: http://takahikokawasaki.github.io/nv-i18n/