If you use Joda Time (and why don't you do it if you have a choice?), It almost got into JDK 1.7), you can do something like this:
String patternForStyleAndLocale = org.joda.time.format.DateTimeFormat.patternForStyle("S-", locale);
Unfortunately, it gives only a two-digit year. To do this, work:
if (!org.apache.commons.lang.StringUtils.contains(patternForStyleAndLocale, "yyyy")) { // The default Joda pattern only has a two digit year for US and Europe, China etc - but we want four digit years patternForStyleAndLocale = StringUtils.replace(patternForStyleAndLocale, "yy", "yyyy"); }
And you can consider caching them in ConcurrentHashMap<Locale, String>
.
The nice thing about entering a numeric date as a pre-localized template is that it does not require further localization later, as it would if you used a template, for example:
"dd MMM yyyy" // UK: "25 Dec 2010" FRANCE: "25 dΓ©c. 2010" etc..
However ... I just noticed from your later comment that you want to pass a JavaScript template - this can become very difficult as JS uses a different template formatting for Java (for example, the ISO date is "yyyy-MM-dd"
in Java and "yy-mm-dd"
in JS). I have not tried to solve this problem, but I would probably use some string mapping in JS or Java to just map Java patterns to JS. You must know each of the patterns that may arise for each of the languages ββin advance.
source share