开发者

Get All Possible Available Currencies

I would like to get all possible avai开发者_运维技巧lable currencies.

Java 7 had provided such feature.

public static Set<java.util.Currency> java.util.Currency.getAvailableCurrencies()

However, I am still using Java 6 for development and deployment. May I know how I can get all possible available currencies? Code example are most welcomed.


After studying the ISO table and the Currency class documentation, it seems that you can ask for currency as code or as Locale; and the class Locale has a getAvailableLocales() method.

So, the code would be:

    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;
    }

Hope this helps.


This may be helpful for those who want to get it as a Java enumeration

    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;
    }
}


Combine the ISO 4217 (xml) definition with your requirements and the sample from openJDK7 if you want all Currencies. If you iterate over the default Locales you only get 78 of 180 defined by ISO 4217.

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" };


I have been using Baltasarq's answer and I find it slows down my app due to a number of java.lang.IllegalArgumentException's being thrown, which is no fault of his, but actually the author of the Currency library, who has elected to throw an exception as opposed to an error (Raw Code).

However looking into that Library, there is a public static method getAvailableCurrencies which will give you all the available currencies as a set without having to loop through all the available locales and throw a bunch of exceptions.

The revised code now looks like this:

public static Set<Currency> getAllCurrencies() {
    Set<Currency> currencies = Currency.getAvailableCurrencies();
    return currencies;
}

Which of course, is silly, you can just as well use the Currency's own static method and save yourself an unnecessary method call. #SaveTheStack


Otherwise a version a little better of Baltasarq code to avoid duplication:

    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);
    }

I have tested on android.


You can also use the library nv-i18n available on maven:

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

To see all currency codes with it, you just have to use:

for (CurrencyCode code : CurrencyCode.values()) {
    System.out.format("[%s] %03d %s\n", code, code.getNumeric(), code.getName());
}

You can see more details about this class and library here: http://takahikokawasaki.github.io/nv-i18n/


Use JodaMoney. Inside joda-money.jar I find MoneyData.csv It has all ISO 4217 currencies. Use org.joda.money.CurrencyUnit to pull currencies.

`

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());
    }

`


I am using the Currency java class but I was not able to find the Currency of Afghanistan (AFN), so I was not sure if I could rely on this Currency Library. To fix this I am using the joda-money library.

So I put in my pom.xml this:

<dependency> <groupId>org.joda</groupId> <artifactId>joda-money</artifactId> <version>0.11</version> </dependency>

And in my code I use this:

List<CurrencyUnit> currencyUnits = CurrencyUnit.registeredCurrencies();

to get all Currencies, and the AFN was there so I am using this library now.

I hope it can help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜