I looked at this recently and did not find an easy way to find this using the standard library. I need to localize the Wicket class org.apache.wicket.extensions.markup.html.form.DateTextField , which accepts a SimpleDateFormat template, not a DateFormat object. Here is the code that I finally decided to use, it does not rely on the details of the JVM implementation, but should analyze the output of DateFormat.format for a specific date (11/22/3333 - in US format).
public static String simpleDateFormatForLocale(Locale locale) { TimeZone commonTimeZone = TimeZone.getTimeZone("UTC"); Calendar c = Calendar.getInstance(commonTimeZone); c.set(3333, Calendar.NOVEMBER, 22); DateFormat localeDateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); localeDateFormat.setTimeZone(commonTimeZone); String dateText = localeDateFormat.format(c.getTime()); return dateText.replace("11", "MM").replace("22", "dd").replace("3333", "yyyy").replace("33", "yyyy"); }
In my case, I want to force yyyy to be used, even if the format returns a two-digit year. That's why I call replace twice a year.
Here is the result for some sample locales:
en_US 11/22/33 MM/dd/yyyy en_CA 22/11/33 dd/MM/yyyy zh_CN 33-11-22 yyyy-MM-dd de 22.11.33 dd.MM.yyyy ja_JP 33/11/22 yyyy/MM/dd
If anyone can improve this, I would like to see their solution.
source share