Issue with DateTimeParseException when using STRICT resolver style

百般思念 提交于 2019-12-01 07:24:55

问题


I am trying to parse a date string using the following pattern: yyMMdd and the STRICT resolver as follows:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT);
LocalDate.parse(expiryDate, formatter);

I get the following DateTimeParseException:

java.time.format.DateTimeParseException: Text '160501' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2016, MonthOfYear=5, DayOfMonth=1},ISO of type java.time.format.Parsed

When I swith to the default resolve style, i.e. ResolverStyle.SMART it allows such dates as 30th of February.

Can someone please help?


回答1:


The strict resolver requires an era to go with YearOfEra. Change your pattern to use "u" instead of "y" and it will work, ie. "uuMMdd".



来源:https://stackoverflow.com/questions/41103603/issue-with-datetimeparseexception-when-using-strict-resolver-style

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!