使用STRICT解析器样式时出现DateTimeParseException问题

bal*_*teo 3 java-8 java-time

我试图使用以下模式解析日期字符串:yyMMddSTRICT解析器如下:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT);
LocalDate.parse(expiryDate, formatter);
Run Code Online (Sandbox Code Playgroud)

我得到以下内容DateTimeParseException:

java.time.format.DateTimeParseException:无法解析文本'160501':无法从TemporalAccessor获取LocalDate:{YearOfEra = 2016,MonthOfYear = 5,DayOfMonth = 1},ISO类型为java.time.format.Parsed

当我切换到默认的解析风格时,即ResolverStyle.SMART允许这样的日期为2月30日.

有人可以帮忙吗?

Jod*_*hen 9

严格的解析器需要一个时代与YearOfEra.将您的模式更改为使用"u"而不是"y",它将起作用,即."uuMMdd".