简体   繁体   English

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

[英]Can't parse String to LocalDate (Java 8)

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.LocalDate variable: 我的输入是以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 DateTimeFormatter JavaDoc , I would expect this to work. 基于DateTimeFormatter JavaDoc ,我希望它能正常工作。 However, I'm greeted with a very friendly and helpful message: 但是,我收到了一条非常友好和有用的信息:

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? 谁能解释我怎么了?

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. 大写字母Y用于星期。 See the javadoc of DateTimeFormatter for more details. 有关更多详细信息,请参见DateTimeFormatterjavadoc

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

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). 这就是说,它不能从TemporalAccessor (提供对日期/时间字段的类似于哈希图的访问的低级接口)创建LocalDate (请求的内容)。

of type java.time.format.Parsed

This is saying that the object passed into the method was of type java.time.format.Parsed . 这就是说,传递给该方法的对象的类型为java.time.format.Parsed This is the standard output type of parsing, and contains all the information that was parsed. 这是解析的标准输出类型,包含所有已解析的信息。

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

This is the toString() form of the java.time.format.Parsed object that resulted from parsing. 这是解析产生的java.time.format.Parsed对象的toString()形式。 It is saying that four things were parsed: 就是说解析了四件事:

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

Since it is not possible to produce a LocalDate from the combination DayOfMonth + MonthOfYear + WeekBasedYear, an exception is thrown. 由于不可能通过DayOfMonth + MonthOfYear + WeekBasedYear的组合来生成LocalDate ,因此将引发异常。

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标准和欧盟,它们从星期一开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM