英文:
Unable to obtain LocalDate from TemporalAccessor
问题
以下是翻译好的部分:
我有以下代码:
final String pattern = "MM/dd/YYYY";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDate localDate = LocalDate.parse(date, formatter);
在尝试解析 String date = "05/29/2003"
时,我收到了一个异常:
Caused by: java.time.DateTimeException: 无法从 TemporalAccessor 获取 LocalDate:{WeekBasedYear[WeekFields[SUNDAY,1]]=2023, DayOfMonth=29, MonthOfYear=5},ISO,类型为 java.time.format.Parsed
导致这个异常的原因是什么?
英文:
I have following code
final String pattern = "MM/dd/YYYY";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDate localDate = LocalDate.parse(date, formatter);
and while trying to parse String date = "05/29/2003"
I receive an exception:
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2023, DayOfMonth=29, MonthOfYear=5},ISO of type java.time.format.Parsed
What is the reason ?
答案1
得分: 4
你的模式不正确,尝试使用以下模式:
final String pattern = "MM/dd/uuuu"; // 或者 "MM/dd/yyyy"
格式化模式区分大小写。"yyyy"代表年份,而"YYYY"代表基于周的年份。因此,你需要使用yyyy
或者更好的选择是uuuu
来解决你的问题。
英文:
Your pattern is not correct instead, try this:
final String pattern = "MM/dd/uuuu"; // or "MM/dd/yyyy"
Formatting patterns are case-sensitive. The "yyyy" represents the year, while "YYYY" represents the week-based year. So you need to use yyyy
or much better uuuu
to solve your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论