java 如果数据库中只有国家/地区代码,则获取货币代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16832655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:06:59  来源:igfitidea点击:

getting currency code if we have only country code in database

javaandroidcurrency

提问by henrycharles

i have problem getting the country currency, i am having the country code in database through these code i have to get the country code in java how do i make the code for this please suggest me. i m trying with this example but its not working.

我在获取国家货币时遇到问题,我正在通过这些代码在数据库中获取国家代码我必须在 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");
    }
}

采纳答案by henrycharles

i found the solution for my problem this is the code i have done.

我找到了我的问题的解决方案,这是我完成的代码。

        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;
    }
}

回答by Puce

Try:

尝试:

//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(); 
}

回答by Glen Best

Debug to determine which currencies are available.

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

    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("----------------------------------------");
    }

If any needed currencies are missing/wrong, follow the instructions from the javadoc:

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

Users can supersede the Java runtime currency data by creating a properties file named <JAVA_HOME>/lib/currency.properties. The contents of the properties file are key/value pairs of the ISO 3166 country codes and the ISO 4217 currency data respectively. The value part consists of three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric code, and a minor unit. Those three ISO 4217 values are separated by commas. The lines which start with '#'s are considered comment lines. For example,

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

#Sample currency properties
JP=JPZ,999,0

will supersede the currency data for Japan.

将取代日本的货币数据。

回答by praveen gowda

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"));

    }

}

回答by Henrique

A simple option that fulfills this requirement:

满足此要求的简单选项:

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;
}

回答by Nacho

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 "";
}