2015-04-06 114 views
1

获取国家代码/名称鉴于我有货币代码,我如何获取货币适用的国家代码/名称?我知道货币和国家之间可能会有1:n的映射关系,那么该如何获得货币适用的所有国家?如何从货币代码

+1

您可以在货币/国家组合存储在数据库中的表。当财务状况发生变化时,您需要更新表格。 – 2015-04-06 11:08:08

+0

不想自己维护映射。这不是一些API吗? – akrohit 2015-04-06 11:09:19

回答

0

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

0

CurrencyCodenv-i18n库有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.