使用STRICT解析器样式时出现DateTimeParseException问题 [英] Issue with DateTimeParseException when using STRICT resolver style

查看:579
本文介绍了使用STRICT解析器样式时出现DateTimeParseException问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下模式解析日期字符串: yyMMdd STRICT 解析程序,如下所示:

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);

我得到以下 DateTimeParseException


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

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

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

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

有人可以帮忙吗?

推荐答案

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

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".

这篇关于使用STRICT解析器样式时出现DateTimeParseException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆