Localtion of pattern for date formats in java

2019-09-03 06:55发布

In java you can get the date format based for a specific locale by doing:

Locale locale = new Locale("en", "UK");
DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
System.out.println( ((SimpleDateFormat)format).toPattern());

This prints

dd-MMM-yyyy

Is this pattern hardcoded in a properties file somewhere? Is there any way I can extend the java mechanism and apply my own pattern (locale dependent) so when I run the above code I get:

dd-MM-yyyy

1条回答
何必那么认真
2楼-- · 2019-09-03 07:47

The idea is to work always with the same format, in my case the DateFormat.SHORT.

I end up using the short format and replacing the default format containing 2 character for the year with 4 characters for the year.

And in case someone want to know, the formats are indeed hardcoded in the package sun.text.resources under the classes FormatData_XX_YY (e.g. FormatData_en_UK).

查看更多
登录 后发表回答