Localtion of pattern for date formats in java

蹲街弑〆低调 提交于 2019-12-11 18:22:46

问题


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:


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).



来源:https://stackoverflow.com/questions/16135398/localtion-of-pattern-for-date-formats-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!