问题
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.
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");
}
}
回答1:
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();
}
回答2:
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:
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,
#Sample currency properties
JP=JPZ,999,0
will supersede the currency data for Japan.
回答3:
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;
}
}
回答4:
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"));
}
}
回答5:
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;
}
回答6:
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 "";
}
来源:https://stackoverflow.com/questions/16832655/getting-currency-code-if-we-have-only-country-code-in-database