Get All Possible Available Currencies

前端 未结 8 1799
忘了有多久
忘了有多久 2021-02-05 09:17

I would like to get all possible available currencies.

Java 7 had provided such feature.

public static Set java.util.Currency.g         


        
8条回答
  •  我寻月下人不归
    2021-02-05 09:35

    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 getAllCurrencies()
        {
            Set toret = new HashSet();
            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.

提交回复
热议问题