I have an application utilizing this class: java.util.Currency
In the application, I maintain a Map of country codes to locales. I retrieve a locale by passing in a country code
When I pass this locale to Currency.getInstance()
, it returns the correct currency. I verified that by calling Currency.getCurrencyCode()
However when I call getDisplayName()
on the same currency instance, it returns null
This behavior does not happen on my dev machine, only on my test server
Both my dev machine and server use Ubuntu 12.04 LTS with:
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.6) (6b27-1.12.6-1ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
The application runs inside tomcat
I could be missing something here
edit
import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
...
public static Map<String, Locale> COUNTRY_TO_LOCALE_MAP;
public static void initCountryToLocaleMap() {
if (COUNTRY_TO_LOCALE_MAP != null) {
return;
}
COUNTRY_TO_LOCALE_MAP = new HashMap<String, Locale>();
Locale[] locales = Locale.getAvailableLocales();
for (Locale l : locales) {
COUNTRY_TO_LOCALE_MAP.put(l.getCountry(), l);
}
}
public static Locale getLocaleFromCountryCode(String countryCode) {
if (countryCode == null) {
return null;
}
initCountryToLocaleMap();
Locale locale = COUNTRY_TO_LOCALE_MAP.get(countryCode);
if (locale == null) {
return new Locale("", countryCode);
}
return locale;
}
public static void main(String args[]) {
initCountryToLocaleMap();
String countryCode = "ZA";
Locale locale = getLocaleFromCountryCode(countryCode);
Currency currency = Currency.getInstance(locale);
System.out.println("currencyCode: " + currency.getCurrencyCode());
System.out.println("displayName: " + currency.getDisplayName());
}
Upon running main(), i got the following output: from dev machine:
currencyCode: ZAR
displayName: South African Rand
from test server:
currencyCode: ZAR