我有获得国家货币的问题,我通过这些代码在数据库中有国家代码我必须在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");
}
}
答案 0 :(得分:6)
尝试:
//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();
}
答案 1 :(得分: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
的属性文件来取代Java运行时货币数据。属性文件的内容分别是ISO 3166国家代码和ISO 4217货币数据的键/值对。值部分由三种ISO 4217货币值组成,即字母代码,数字代码和次要单元。这三个ISO 4217值以逗号分隔。以'#开头的行被视为注释行。例如,
#Sample currency properties
JP=JPZ,999,0
将取代日本的货币数据。
答案 2 :(得分: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;
}
}
答案 3 :(得分: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"));
}
}
答案 4 :(得分: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;
}
答案 5 :(得分: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 "";
}