-2

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.

Currency.getInstance(request.getlocale())

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

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sri
  • 1,205
  • 2
  • 21
  • 52

1 Answers1

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.

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.

Community
  • 1
  • 1
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190