userx
userx

Reputation: 3753

Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor:

I have written a simple piece of code to parse a date using java 8 api. I also went through various other stack overflow questions on this topic but have not been able to resolve the error.

package com.test.java8api;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, MM/DD/YYYY - HH:mm", Locale.ENGLISH).withResolverStyle(ResolverStyle.STRICT);
        LocalDateTime date = LocalDateTime.parse("Sun, 04/22/2018 - 09:45",formatter);
        System.out.println(date);
    }

}

The error log is

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sun, 04/22/2018 - 09:45' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=4, DayOfYear=22},ISO resolved to 09:45 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at com.test.java8api.Test.main(Test.java:13)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=4, DayOfYear=22},ISO resolved to 09:45 of type java.time.format.Parsed
    at java.time.LocalDateTime.from(LocalDateTime.java:461)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=4, DayOfYear=22},ISO resolved to 09:45 of type java.time.format.Parsed
    at java.time.LocalDate.from(LocalDate.java:368)
    at java.time.LocalDateTime.from(LocalDateTime.java:456)
    ... 4 more

Can you please help me out on the same ?

EDIT:

I have been asked whether it is a possible duplicate of Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)

which is not the case as the aforementioned question talks about the difference of usage of LocalDate and LocalDateTime, while the current question is on using the right symbols or alphabets for a pattern.

Upvotes: 2

Views: 10106

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338516

Formatting patterns are case-sensitive

"EEE, MM/DD/YYYY - HH:mm"

Read the documentation carefully to learn that formatting pattern codes are case-sensitive.

  • DD means day-of-year. dd means day-of-month.
  • YYYY means week-based-year. yyyy (and uuuu) mean calendar year.

Please take more care before posting to Stack Overflow. You could have found hundreds of working code examples already posted on Stack Overflow to show the faults in your code.


Tip: Avoid custom formats such as that seen in your Question. When serializing date-time values to text, always use the standard ISO 8601 formats. They are surprisingly practical and useful, designed to be unambiguous, easy to parse by machine, and easy to read by humans across cultures.

The java.time classes use the ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.

Upvotes: 7

Related Questions