Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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:

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

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

like image 829
mthmulders Avatar asked Jul 24 '15 08:07

mthmulders


People also ask

How do I LocalDate to a string?

The toString() method of a LocalDate class is used to get this date as a String, such as 2019-01-01. The output will be in the ISO-8601 format uuuu-MM-dd. Parameters: This method does not accepts any parameters. Return value: This method returns String which is the representation of this date, not null.

What exception does LocalDate parse throw?

parse(CharSequence text, DateTimeFormatter formatter) Return value: This method returns LocalTime which is the parsed local date-time. Exception: This method throws DateTimeParseException if the text cannot be parsed.

Can we convert string to date in Java?

We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes.

How do I parse LocalDateTime?

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime. parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.


2 Answers

For year you have to use the lowercase y:

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

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

like image 76
Jens Avatar answered Oct 19 '22 18:10

Jens


The answer to the question is to use 'y' not '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).

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.

{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:

  • 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)

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

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.

like image 23
JodaStephen Avatar answered Oct 19 '22 18:10

JodaStephen