Java中日期格式的模式定位 [英] Localtion of pattern for date formats in java
问题描述
在Java中,您可以通过执行以下操作来获取基于特定语言环境的日期格式:
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());
此打印
dd-MMM-yyyy
此模式是否在某个位置的属性文件中进行了硬编码?有什么方法可以扩展java机制并应用自己的模式(取决于语言环境),所以当我运行上述代码时,我得到:
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
推荐答案
想法是始终使用相同的格式,在我的情况下为DateFormat.SHORT.
The idea is to work always with the same format, in my case the DateFormat.SHORT.
我最终使用了短格式,并用包含4个字符的年份替换了包含2个字符的默认格式.
I end up using the short format and replacing the default format containing 2 character for the year with 4 characters for the year.
万一有人想知道,这些格式确实被硬编码在软件包sun.text.resources中的FormatData_XX_YY类(例如FormatData_en_UK)下.
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).
这篇关于Java中日期格式的模式定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!