7

我在获取国家货币时遇到问题,我通过这些代码在数据库中获取国家代码我必须在 java 中获取国家代码我如何为此制作代码请建议我。我正在尝试这个例子,但它不起作用。

    class Utils {
    public static SortedMap<Currency, Locale> currencyLocaleMap;
    static {
        currencyLocaleMap = new TreeMap<Currency, Locale>(
                new Comparator<Currency>() {
                    public int compare(Currency c1, Currency c2) {
                        return c1.getCurrencyCode().compareTo(
                                c2.getCurrencyCode());
                    }
                });
        for (Locale locale : Locale.getAvailableLocales()) {
            try {
                Currency currency = Currency.getInstance(locale);
                currencyLocaleMap.put(currency, locale);
            } catch (Exception e) {
            }
        }
    }

    public static String getCurrencySymbol(String currencyCode) {
        Currency currency = Currency.getInstance(currencyCode);
        System.out.println(currencyCode + ":-"
                + currency.getSymbol(currencyLocaleMap.get(currency)));
        return currency.getSymbol(currencyLocaleMap.get(currency));
    }
}

  public class GetCurrency {
    public static void main(String[] args) {

        Utils.getCurrencySymbol(Currency.getInstance("INR").getCurrencyCode());
        Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN)
                .getCurrencyCode());
        Utils.getCurrencySymbol(Currency.getInstance(Locale.UK)
                .getCurrencyCode());

        Utils.getCurrencySymbol("INR");
    }
}
4

6 回答 6

12

尝试:

//to retrieve currency code
public static String getCurrencyCode(String countryCode) {
    return Currency.getInstance(new Locale("", countryCode)).getCurrencyCode(); 
}

//to retrieve currency symbol 
public static String getCurrencySymbol(String countryCode) {
    return Currency.getInstance(new Locale("", countryCode)).getSymbol(); 
}
于 2013-05-30T10:12:18.473 回答
2

调试以确定哪些货币可用。

    Set<Currency> avail = Currency.getAvailableCurrencies();
    for (Currency next : avail) {
        System.out.println("----------------------------------------");
        System.out.println("displayName="+next.getDisplayName());
        System.out.println("currencyCode="+next.getCurrencyCode());
        System.out.println("numericCode="+next.getNumericCode());
        System.out.println("symbol="+next.getSymbol());
        System.out.println("toString="+next.toString());
        System.out.println("----------------------------------------");
    }

如果任何需要的货币丢失/错误,请按照 javadoc 中的说明进行操作:

用户可以通过创建一个名为<JAVA_HOME>/lib/currency.properties. 属性文件的内容分别是 ISO 3166 国家代码和 ISO 4217 货币数据的键/值对。值部分由三个 ISO 4217 货币值组成,即字母代码、数字代码和次要单位。这三个 ISO 4217 值用逗号分隔。以'#' 开头的行被视为注释行。例如,

#Sample currency properties
JP=JPZ,999,0

将取代日本的货币数据。

于 2013-05-30T10:14:21.023 回答
1

我找到了解决我的问题的方法,这是我所做的代码。

        public static Map<Currency, Locale> getCurrencyLocaleMap() {
        Map<Currency, Locale> currencyLocaleMap = new HashMap<Currency, Locale>();
        for (Locale locale : Locale.getAvailableLocales()) {
            try {
                Currency currency = Currency.getInstance(locale);
                currencyLocaleMap.put(currency, locale);
            } catch (Exception e) {
            }
        }
        return currencyLocaleMap;
    }

    public static String getCurrencySymbol(String currencyCode) {
        String currencySymbol = null;
        if (currencyCode == null || currencyCode.isEmpty()) {
            currencySymbol = currencyCode;
        } else {
            Locale currencyLocale = null;
            Map<Currency, Locale> currencyLocaleMap = null;
            Currency currency = null;
            try {
                currency = Currency.getInstance(currencyCode);
                currencyLocaleMap = getCurrencyLocaleMap();
                currencyLocale = currencyLocaleMap.get(currency);
            } catch (Exception e) {
                System.out.println("No symbol is there for currencyCode="
                        + currencyCode);
            }
            if (currency != null && currencyLocale != null) {
                currencySymbol = currency.getSymbol(currencyLocaleMap
                        .get(currency));
            } else {
                currencySymbol = currencyCode;
            }
        }
        return currencySymbol;
    }
}
于 2013-06-03T12:22:40.510 回答
1
import java.util.Currency;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.LocaleUtils;

public class Test {


    public static String getCurrenyCodeByCountryCode(String countryCode) {

        String currencyCode="USD";

        try {
            List<Locale> locales=LocaleUtils.languagesByCountry(countryCode);

            Currency currency=Currency.getInstance(locales.get(0));

            currencyCode=currency.getCurrencyCode();


        } catch (Exception e) {

        }
        return currencyCode;

    }


    public static void main(String[] args) throws Exception {


        System.out.println(getCurrenyCodeByCountryCode("IN"));

    }

}
于 2016-05-28T08:34:25.833 回答
1

满足此要求的简单选项:

public static String getCurrencySymbol(String countryCode) {
    String currencySymbol = "";
    Locale locale = null;
    Currency currency = null;
    try {
        locale = new Locale("", countryCode);
        currency = Currency.getInstance(locale);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

    if (currency != null) {
        currencySymbol = currency.getCurrencyCode();
    }

    return currencySymbol;
}
于 2017-06-30T15:32:36.057 回答
1
public static String getSymbol(String countryCode) {
    Locale[] availableLocales = Locale.getAvailableLocales();
    for (int i = 0; i < availableLocales.length; i++) {
        if (availableLocales[i].getCountry().equalsIgnoreCase(countryCode))
            return Currency.getInstance(availableLocales[i]).getCurrencyCode();
    }
    return "";
}
于 2017-09-13T15:47:43.430 回答