To format internationalized data properly, Java has a bunch of handy features. If you're English, you'll probably be setting your Locale to Locale.ENGLISH
and all your i18n messages will come out fine, numbers, percentages, currencies etc. look right, and it all pretty much works. However...
Beanshell> import java.text.*;
Beanshell> DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
Beanshell> df.format(new Date());
Sunday, February 26, 2018
What?! That's not an English date, that's one of those silly colonial formatted dates with the numbers the wrong way round. The cheek!
Yes, in Java world Locale.ENGLISH
means American so you get American formatted dates (month and day the wrong way round). Ah well, I guess that's a perk of creating the software in the first place. If you're English and you want English formatted dates, you'll need to use Locale.UK
. That sets the locale to "en_GB", i.e. English language and GB formatting)
Beanshell> import java.text.*;
Beanshell> DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.UK);
Beanshell> df.format(new Date());
Sunday, 26 February 2018
Ahh, that's better, I can sip my tea and watch the cricket in peace again.
In general, if you are English and want Java to format things correctly for you, don't use Locale.ENGLISH
and don't just set the language code to "en", instead use Locale.UK
and the language code "en_GB" (or "en-GB", see my other article about that particular annoyance)