【发布时间】:2021-10-13 07:15:06
【问题描述】:
我正在尝试将一些日期字符串解析为日期值,但是,使用下面的代码,我得到了一个异常:
我的代码
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("MMMM-YYYY"))
.toFormatter(Locale.ENGLISH);
LocalDate KFilter = LocalDate.parse("August-2021", formatter);
错误日志是
java.time.format.DateTimeParseException: Text 'August-2021' could not be parsed:
Unable to obtain LocalDate from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2021, MonthOfYear=8},
ISO of type java.time.format.Parsed
你能帮我解决一下吗?
【问题讨论】:
-
使用
y代替Y,然后使用parseDefaulting作为月份的默认值。关于这个问题已经有几十个问题了。