如果我有一个货币代码,如何获取适用该货币的国家代码/名称?我知道货币和国家之间可能存在一对多的映射关系,那么如何获取应用该货币的所有国家?
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.