加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java-8 – 使用STRICT解析器样式时出现DateTimeParseException问

发布时间:2020-12-15 04:37:02 所属栏目:Java 来源:网络整理
导读:我试图使用以下模式解析日期字符串:yyMMdd和STRICT解析器,如下所示: DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT);LocalDate.parse(expiryDate,formatter); 我得到以下DateTimeParseE
我试图使用以下模式解析日期字符串:yyMMdd和STRICT解析器,如下所示:

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

我得到以下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

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

有人可以帮忙吗?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读