java字符串MonthYear到本地日期
我试图将某个日期字符串解析为日期值,但是,使用以下代码,我得到一个异常:
我的代码
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
你能帮我解决这个问题吗
# 1 楼答案
输出:
你的代码出了什么问题
您的代码有两个问题:
YYYY
表示基于周的年份,仅对周数有用。使用小写yyyy
或uuuu
李>LocalDate
。我建议您解析为YearMonth
,然后转换。在转换中,您需要在月份的某一天进行决策。另一种方法是通过DateTimeFormatterBuilder.parseDefaulting()
指定月份的某一天李>链接
相关问题: