Currency based on HTTP request getLocale throwing IllegalArgumentException


Currency based on HTTP request getLocale throwing IllegalArgumentException



I am trying to get currency symbols based on locale, currency code in a servlet.



I tried to do the following but it is throwing IllegalArgumentException.


IllegalArgumentException


Currency.getInstance(request.getlocale())



Locale has language as "en" but country value empty.



How to get currency symbols for a locale, currency code?





Please post a code snippet.
– Jared Stewart
Jun 30 at 0:39




1 Answer
1



It does not make sense to ask for the currency for a locale without a country.



What is the currency for, say, "English", as in your question? This could be USD (US), AUD (Australia), UKP (United Kingdom), EUR (Ireland), CAD (Canada), etc., etc. In other words, without knowing the country you cannot pick a currency, and in fact, the currency depends ONLY on the country and never on the language. Countries have one official currency but can have more than one language, as in Canada having locales fr_CA and en_CA.


fr_CA


en_CA



Also, it's pretty clearly spelled out in the Javadoc (my emphasis):


public static Currency getInstance​(Locale locale)



Returns the Currency instance for the country of the given locale. The language and variant components of the locale are ignored. The result may vary over time, as countries change their currencies. For example, for the original member countries of the European Monetary Union, the method returns the old national currencies until December 31, 2001, and the Euro from January 1, 2002, local time of the respective countries.



The method returns null for territories that don't have a currency, such as Antarctica.



Parameters:



    locale - the locale for whose country a Currency instance is needed



Returns:



    the Currency instance for the country of the given locale, or null
Throws:



    NullPointerException - if locale is null



    IllegalArgumentException - if the country of the given locale is not a supported ISO 3166 country code.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV