java DateTimeFormatter严格与宽松的意外行为
我有一些text
,只有在解析样式为Strict
时才被DateTimeFormatter
解析,而不是在Lenient
时
这似乎与我的预期相反
例如:
String pattern = "ddMMyyHH:mm:ss";
String text = "02011104:21:32";
System.out.println(MessageFormat.format("Strict - {0}", new DateTimeFormatterBuilder().parseStrict().appendPattern(pattern).toFormatter().parse(text)));
System.out.println(MessageFormat.format("Lenient - {0}", new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter().parse(text)));
输出:
Strict - {},ISO resolved to 2011-01-02T04:21:32
Exception in thread "main" java.time.format.DateTimeParseException: Text '02011104:21:32' could not be parsed at index 8
# 1 楼答案
将此作为bug发布后-https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8228353
我得到的答复如下: