无法将字符串解析为 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
Can't parse String to LocalDate (Java 8)
提问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.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. However, I'm greeted with a very friendly and helpful message:
基于DateTimeFormatter
JavaDoc,我希望这能奏效。然而,我收到了一条非常友好和有用的信息:
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 用于表示星期。有关更多详细信息,请参阅DateTimeFormatter的javadoc。
回答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).
这就是说它不能LocalDate
从TemporalAccessor
(提供对日期/时间字段的类似哈希图的访问的低级接口)创建(所请求的)。
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.Parsed
object 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 of1
MonthOfYear=7
, the month-of-year parsed with value of7
WeekBasedYear[WeekFields[MONDAY,4]]=2015
, the week-based-year parsed with value of2015
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 LocalDate
from 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 标准和欧盟中,它们从周一开始。