如何从货币代码中获取国家/地区代码/名称

时间:2015-04-06 11:04:52

标签: java android

鉴于我有货币代码,如何获取货币适用的国家/地区代码/名称?我理解货币和国家之间可能存在1:n映射,在这种情况下如何获得适用该货币的所有国家/地区?

2 个答案:

答案 0 :(得分:1)

nv-i18n库中的

CurrencyCode类具有getCountryList()方法,该方法返回使用该货币的国家/地区列表。下面是一个示例命令行应用程序。

import java.util.List;
import com.neovisionaries.i18n.CountryCode;
import com.neovisionaries.i18n.CurrencyCode;

public class CurrencyToCountryList
{
    public static void main(String[] args)
    {
        // For each currency code such as EUR and USD.
        for (String code : args)
        {
            // Get a CurrencyCode instance.
            CurrencyCode currency =
                CurrencyCode.getByCode(code);

            if (currency == null)
            {
                // The code is invalid.
                System.out.format("'%s' is invalid.\n", code);
                continue;
            }

            System.out.format("%s (%s) is used by:\n",
                currency, currency.getName());

            // Get the list of countries using the currency.
            List<CountryCode> countryCodeList =
                currency.getCountryList();

            // For each country.
            for (CountryCode country : countryCodeList)
            {
                // Print the country code and the country name.
                System.out.format("\t%s: %s\n",
                    country, country.getName());
            }
        }
    }
}

如果你像这样运行这个应用程序:

$ java -cp nv-i18n-1.15.jar:. CurrencyToCountryList USD

您将得到如下所示的结果:

USD (US Dollar) is used by:
  AS: American Samoa
  BQ: Bonaire, Sint Eustatius and Saba
  EC: Ecuador
  FM: Micronesia, Federated States of
  GU: Guam
  HT: Haiti
  IO: British Indian Ocean Territory
  MH: Marshall Islands
  MP: Northern Mariana Islands
  PA: Panama
  PR: Puerto Rico
  PW: Palau
  SV: El Salvador
  TC: Turks and Caicos Islands
  TL: Timor-Leste
  UM: United States Minor Outlying Islands
  US: United States
  VG: Virgin Islands, British
  VI: Virgin Islands, U.S.

答案 1 :(得分:0)

为了获得货币的所有国家/地区,您可以在Java中对MultiMap对象进行简单搜索。通过运行for来搜索地图(比如第一个字符串是货币,第二个是国家)和if来检查货币是否是您要查找的货币。如果是这样,您可以将所有找到的国家/地区添加到数组中,然后再使用它们。