下面分别对 不同的 dateStyle 和 timeStyle 设置不同的值 做了对比: dateStyle="medium" timeStyle="medium" will show 2020-8-19 17:27:01 dateStyle="medium" timeStyle="short" will show 2020-8-19 下午5:27 dateStyle="medium" timeStyle="long" will show 2020-8-19 下午05时27分01秒 dateStyle="long" timeStyle="long" will show 2020年8月19日 下午05时27分01秒 dateStyle="short" timeStyle="long" will show 20-8-19 下午05时27分01秒
下面我们来解决这个问题, 我们从代码来分析. 为什么 dateStyle="medium" timeStyle="medium" will show 2020-8-19 17:27:01 dateStyle="medium" timeStyle="short" will show 2020-8-19 下午5:27 medium 和 short 显示的是那样的格式, 而不是这样的格式..... short 字面意思是短,感觉这里应该是想显示个短格式的时间的,那你把秒去掉不就得了,干嘛显示个中文 何来 短 呢? 感觉更长了呢...
进入到 java/util/ResourceBundle.java private static ResourceBundle getBundleImpl(String baseName, Locale locale, ClassLoader loader, Control control) if (locale == null || control == null) { throw new NullPointerException(); }
// We create a CacheKey here for use by this call. The base // name and loader will never change during the bundle loading // process. We have to make sure that the locale is set before // using it as a cache key. CacheKey cacheKey = new CacheKey(baseName, locale, loader); ResourceBundle bundle = null;
public abstract class ResourceBundle 这个是个抽象类, 发现他有很多子类. sun.text.resources.cldr.zh.FormatData_zh sun.text.resources.zh.FormatData_zh
在文件 https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/text/resources/zh/FormatData_zh.java public class FormatData_zh extends ParallelListResourceBundle { public FormatData_zh() { }
protected final Object[][] getContents() { String[] rocEras = new String[]{"民国前", "民国"}; return new Object[][] } }
我们选取几个 语言下面的,TimePatterns 和 DatePatterns的值比较看看. 这2个值就是格式日期时间的. https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/text/resources/zh/FormatData_zh.java { "TimePatterns", new String[] { "ahh'\u65f6'mm'\u5206'ss'\u79d2' z", // full time pattern "ahh'\u65f6'mm'\u5206'ss'\u79d2'", // long time pattern "H:mm:ss", // medium time pattern "ah:mm", // short time pattern } }, { "DatePatterns", new String[] { "yyyy'\u5e74'M'\u6708'd'\u65e5' EEEE", // full date pattern "yyyy'\u5e74'M'\u6708'd'\u65e5'", // long date pattern "yyyy-M-d", // medium date pattern "yy-M-d", // short date pattern } }, https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/text/resources/zh/FormatData_zh_HK.java { "TimePatterns", new String[] { "ahh'\u6642'mm'\u5206'ss'\u79d2' z", // full time pattern "ahh'\u6642'mm'\u5206'ss'\u79d2'", // long time pattern "ahh:mm:ss", // medium time pattern "ah:mm", // short time pattern } }, { "DatePatterns", new String[] { "yyyy'\u5e74'MM'\u6708'dd'\u65e5' EEEE", // full date pattern "yyyy'\u5e74'MM'\u6708'dd'\u65e5' EEEE", // long date pattern "yyyy'\u5e74'M'\u6708'd'\u65e5'", // medium date pattern "yy'\u5e74'M'\u6708'd'\u65e5'", // short date pattern } }, https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/text/resources/zh/FormatData_zh_TW.java { "TimePatterns", new String[] { "ahh'\u6642'mm'\u5206'ss'\u79d2' z", // full time pattern "ahh'\u6642'mm'\u5206'ss'\u79d2'", // long time pattern "a hh:mm:ss", // medium time pattern "a h:mm", // short time pattern } }, { "DatePatterns", new String[] { "yyyy'\u5e74'M'\u6708'd'\u65e5' EEEE", // full date pattern "yyyy'\u5e74'M'\u6708'd'\u65e5'", // long date pattern "yyyy/M/d", // medium date pattern "yyyy/M/d", // short date pattern } },
再选取其他国家语言的 https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/text/resources/ja/FormatData_ja.java { "TimePatterns", new String[] { "H'\u6642'mm'\u5206'ss'\u79d2' z", // full time pattern "H:mm:ss z", // long time pattern "H:mm:ss", // medium time pattern "H:mm", // short time pattern } }, { "DatePatterns", new String[] { "yyyy'\u5e74'M'\u6708'd'\u65e5'", // full date pattern "yyyy/MM/dd", // long date pattern "yyyy/MM/dd", // medium date pattern "yy/MM/dd", // short date pattern } }, https://github.com/openjdk/jdk/blob/jdk8-b120/jdk/src/share/classes/sun/text/resources/en/FormatData_en_GB.java { "TimePatterns", new String[] { "HH:mm:ss 'o''clock' z", // full time pattern "HH:mm:ss z", // long time pattern "HH:mm:ss", // medium time pattern "HH:mm", // short time pattern } }, { "DatePatterns", new String[] { "EEEE, d MMMM yyyy", // full date pattern "dd MMMM yyyy", // long date pattern "dd-MMM-yyyy", // medium date pattern "dd/MM/yy", // short date pattern } },
[zh_CN]DateFormat: Incorrect hour representation in standard time format pattern Description Java: 5, 6, 7 all have this problem. It's not a regression.
In S. Chinese(zh_CN), When the time of the date is at 0:00, the midnight of a day, and using DateFormat to format the date object, the output woulde be ShangWu 12:00, rather than 00:00. This is very misleading in Chinese, because in China, there is no covention that localized 12:00 am means 0:00 at midnight.
In FormatData_zh_CN.java, ah is used in DateTimePattern. This follows the standard time pattern in CLDR. I check the national standard for date and time in China, GB/T 7408-2005. There is no hour based on 1 to 12. hh means 00 to 24, not 1 to 12 or 0 to 11. So, in zh_CN, HH should be used to replace ah in standard time pattern. This change also fits the national standard.
I also reported the defect to unicode.org via http://www.unicode.org/reporting.html.
Attached is the source code of the program that can reproduce the problem in zh_CN, zh_TW, and ko. I am sure that it's a defect in zh_CN because I am the native speaker and check the national standard published by the government, but for zh_TW and ko, I am not sure.
The bug report I submitted to unicode is also attached. Confirmed with language specialists that this problem is zh_CN only. Current time format is OK in zh_TW and ko.
Activity All Comments Work Log History Activity Ascending order - Click to sort in descending order Permalink yhuang Yong Huang (Inactive) added a comment - 2011-09-06 00:08 BT2:EVALUATION
To fix the problem, FormatData_zh_CN.java needs to be updated. ah should be replace by HH.
I need to further confirm if it's a defect in zh_TW and ko. Permalink yhuang Yong Huang (Inactive) added a comment - 2011-11-22 18:25 BT2:EVALUATION
Set the status to imcomplete, because current implementation in Java is in accordance with CLDR.
The resource file will not be modified until any change is published in CLDR. Permalink yhuang Yong Huang (Inactive) added a comment - 2011-11-23 17:55 BT2:EVALUATION
Will wait until http://unicode.org/cldr/trac/ticket/4108 is solved in CLDR. Permalink yhuang Yong Huang (Inactive) added a comment - 2013-01-06 19:21 According to the CLDR ticket, this will not be fixed in CLDR:
"I talked to three of our Chinese speaking engineers without getting any consensus that this was an actual problem. It seemed to be converging more on being a user preference than a real issue.
This pattern is also all over the zh patterns. Given this, I won't kick it back, but will defer it."