【发布时间】:2017-04-27 11:33:03
【问题描述】:
我正在尝试使用以下模式解析日期字符串:yyMMdd 和 STRICT 解析器如下:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT);
LocalDate.parse(expiryDate, formatter);
我收到以下DateTimeParseException:
java.time.format.DateTimeParseException: 文本 '160501' 不能 已解析:无法从 TemporalAccessor 获取 LocalDate: {YearOfEra=2016, MonthOfYear=5, DayOfMonth=1},ISO 类型 java.time.format.Parsed
当我切换到默认解析样式时,即ResolverStyle.SMART 它允许日期为 2 月 30 日。
有人可以帮忙吗?
【问题讨论】:
-
我很遗憾
java.time-design 不够聪明,无法认识到 - 在没有时代字段的情况下 - 时代(符号 y)应该像预测公历年(符号ü)。此外,原始的 ISO-8601 文件没有提及时代。因此,真正应该像 ISO 所说的那样处理简单的年月日组合,忽略时代并使用预测的公历。