无法从TemporalAccessor获取LocalDate。

huangapple go评论55阅读模式
英文:

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.


huangapple
  • 本文由 发表于 2023年5月24日 19:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定