Cheok Yan Cheng Cheok Yan Cheng - 3 months ago 21
Java Question

Get All Possible Available Currencies

I would like to get all possible available 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.

Answer

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 {
                toret.add( Currency.getInstance( loc ) );
            } catch(Exception exc)
            {
                // Locale not found
            }
        }

        return toret;
    }

Hope this helps.