Get All Possible Available Currencies

前端 未结 8 1652
忘了有多久
忘了有多久 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:27

    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 getAllCurrencies() {
        Set 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

提交回复
热议问题