Jul 27

You can use the getDisplayName() in the Calendar class to get a localized (according to Locale) string for any of the Calendar fields.

For example to get the day name you can use the following:

Calendar cal = Calendar.getInstance();
String day = cal.getDisplayName(Calendar.DAY_OF_WEEK,
   Calendar.LONG, Locale.getDefault())

written by objects \\ tags: , ,

Nov 15

The BigDecimal constructors do not take the Locale into account when parsing number strings.
This means the following code will throw a NumberFormatException

Locale.setDefault(new Locale("nl", "NL"));
String s =  "2.343.298,09324798";
BigDecimal bd = new BigDecimal(s);

To parse localized strings as BigDecimal we instead need to use the DecimalFormat class

		Locale.setDefault(new Locale("nl", "NL"));
		String s =  "2.343.298,09324798";
		DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
		df.setParseBigDecimal(true);
		BigDecimal bd = (BigDecimal) df.parse(s);

written by objects \\ tags: , , , ,

Apr 29

Sometimes you need to get the currency symbol used for your currency in a different Locale. eg. In the US they use $, but a different Locale may use US$.

The Currency class can be used to not only get the currency symbol for a Locale, but also the currency symbol used for your currency in a different Locale.

Currency currency = Currency.getInstance(myLocale);
String symbol = currency.getSymbol(otherLocale);

written by objects \\ tags: , ,