无法将字符串解析为 LocalDate (Java 8)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31605411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:51:27  来源:igfitidea点击:

Can't parse String to LocalDate (Java 8)

javadatetimejava-8java-time

提问by mthmulders

My input is a String representation of a date in the format "01-07-2015" for July 1, 2015. I'm trying to parse this into a java.time.LocalDatevariable:

我的输入是 2015 年 7 月 1 日格式为“01-07-2015”的日期的字符串表示形式。我试图将其解析为一个java.time.LocalDate变量:

final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-YYYY");
final String input = "01-07-2015";
final LocalDate localDate = LocalDate.parse(input, DATE_FORMAT);

Based on the DateTimeFormatterJavaDoc, I would expect this to work. However, I'm greeted with a very friendly and helpful message:

基于DateTimeFormatterJavaDoc,我希望这能奏效。然而,我收到了一条非常友好和有用的信息:

Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=1, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2015},ISO of type java.time.format.Parsed

引起:java.time.DateTimeException:无法从TemporalAccessor获取LocalDate:{DayOfMonth=1,MonthOfYear=7,WeekBasedYear[WeekFields[MONDAY,4]]=2015},java.time.format.Parsed类型的ISO

I don't really understand what this exception is telling me. Can anyone explain me what's going wrong?

我真的不明白这个例外告诉我什么。谁能解释一下出了什么问题?

回答by Jens

For year you have to use the lowercase y:

对于年份,您必须使用小写的 y:

final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("dd-MM-yyyy");

Uppercase Y is used for weekyear. See the javadoc of DateTimeFormatterfor more details.

大写 Y 用于表示星期。有关更多详细信息,请参阅DateTimeFormatterjavadoc

回答by JodaStephen

The answer to the question is to use 'y' not 'Y'.

问题的答案是使用“y”而不是“Y”。

To explain the error message, lets decompose it:

为了解释错误信息,让我们分解它:

Unable to obtain LocalDate from TemporalAccessor

Unable to obtain LocalDate from TemporalAccessor

This is saying that it cannot create a LocalDate(what was requested) from a TemporalAccessor(the low-level interface that provides hashmap-like access to the fields of date/time).

这就是说它不能LocalDateTemporalAccessor(提供对日期/时间字段的类似哈希图的访问的低级接口)创建(所请求的)。

of type java.time.format.Parsed

of type java.time.format.Parsed

This is saying that the object passed into the method was of type java.time.format.Parsed. This is the standard output type of parsing, and contains all the information that was parsed.

这就是说传递给方法的对象的类型是java.time.format.Parsed。这是解析的标准输出类型,包含所有被解析的信息。

{DayOfMonth=1, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2015},ISO

{DayOfMonth=1, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2015},ISO

This is the toString()form of the java.time.format.Parsedobject that resulted from parsing. It is saying that four things were parsed:

这是解析产生toString()java.time.format.Parsed对象的形式。据说解析了四件事:

  • DayOfMonth=1, the day-of-month parsed with value of 1
  • MonthOfYear=7, the month-of-year parsed with value of 7
  • WeekBasedYear[WeekFields[MONDAY,4]]=2015, the week-based-year parsed with value of 2015
  • ISO, which is the ISO calendar system (a default value)
  • DayOfMonth=1, 用值解析的月份日期 1
  • MonthOfYear=7,用值解析的月份 7
  • WeekBasedYear[WeekFields[MONDAY,4]]=2015, 以周为基础的年份解析为 2015
  • ISO,这是 ISO 日历系统(默认值)

Since it is not possible to produce a LocalDatefrom the combination DayOfMonth + MonthOfYear + WeekBasedYear, an exception is thrown.

由于不可能LocalDate从 DayOfMonth + MonthOfYear + WeekBasedYear 组合中生成 a ,因此会引发异常。

Note that the [WeekFields[MONDAY,4]]part refers to the fact that there are many different ways to define a week, in the US weeks start on Sunday, but in the ISO standard and the EU they start on Monday.

请注意,该[WeekFields[MONDAY,4]]部分指的是有许多不同的方式来定义一周,在美国,周从周日开始,但在 ISO 标准和欧盟中,它们从周一开始。