DateTimeFormatter-严格vs宽松的意外行为

时间:2019-07-15 13:51:40

标签: java datetime java.time

我有一些text仅在解析样式为DateTimeFormatter时由Strict解析,而不是在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 个答案:

答案 0 :(得分:0)