0
votes

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
2
Please show code. We can't help you without something concrete to look atStormeHawke
check the default locale on both of your server (DEV and TEST)Sajan Chandran
Are you sure that your dev and test machine use the same JDK version?David Levesque

2 Answers

0
votes

The javadoc for the Currency class indicates that method getDisplayName() is new in version 1.7 of Java. Try running Tomcat with version 1.7 of the JDK.

I would be surprised if Tomcat just silently fails and returns null. Did you check in the Tomcat logs to see if there is any Exception?

0
votes

You are calling:

Locale.getAvailableLocales();

To populate you COUNTRY_TO_LOCALE_MAP map with key as country and value as Locale.

Problem is that there is no one-to-one mapping between all countries in the world and the locales. e.g. You will get many Local entries with empty country in the list. You are inserting an empty key multiple times into your map as a result of that behavior.

And yes the country you're looking up ZA is also not getting returned. That is the reason you're getting null returned.

Locales actually represent regions. They are meant to mark things like language, currency etc.