How to Obtain Localdatetime from Temporalaccessor When Parsing Localdatetime (Java 8)

Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)

It turns out Java does not accept a bare Date value as DateTime. Using LocalDate instead of LocalDateTime solves the issue:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dt = LocalDate.parse("20140218", formatter);

Unable to obtain LocalTime from TemporalAccessor

It has to be dd-MM-yyyy HH:mm:ss

 h       clock-hour-of-am-pm (1-12)  number            12
H hour-of-day (0-23) number 0

Because in your example, the 12 can either be AM or PM.


The formatter cannot decide between AM or PM as the information is missing:

 a       am-pm-of-day                text              PM

Both are possible, thus the error Unable to obtain LocalTime occurs.

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

I can only reproduce the exception you get when I try to parse to a LocalDateTime, so I assume that's what you want.

Your mistake is using hh (clock-hour-of-am-pm) instead of HH (hour-of-day). This works:

LocalDateTime ldt = LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt);

And prints:

2017-02-02T08:59:12

Exception Unable to obtain LocalDateTime from TemporalAccessor while parsing DateTime using LocalDateTime

You don't specify a year in the input String that you want to convert to a LocalDateTime.

A LocalDateTime has to be necessarily be associated to a year.

So the following exception is thrown :

Unable to obtain LocalDate from TemporalAccessor: {DayOfYear=365},ISO
resolved to 23:11:09 of type java.time.format.Parsed

You could set a fake date as input as you overwrite it with : withYear(2016) :

String stringInput = "02000365231109";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("0yyyyDDDHHmmss");
LocalDateTime date2 = LocalDateTime.parse(stringInput, formatter2).withYear(2016);

If you cannot modify directly the input you could create a new String with the correct format before invoking the parse() method :

String stringInput = "0365231109";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("0yyyyDDDHHmmss");
stringInput = "02000" + stringInput.substring(1);
LocalDateTime date2 = LocalDateTime.parse(stringInput, formatter2).withYear(2016);

How can I parse/format dates with LocalDateTime? (Java 8)

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.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Formatting date and time

To create a formatted string out a LocalDateTime object you can use the format() method.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

Note that there are some commonly used date/time formats predefined as constants in DateTimeFormatter. For example: Using DateTimeFormatter.ISO_DATE_TIME to format the LocalDateTime instance from above would result in the string "1986-04-08T12:30:00".

The parse() and format() methods are available for all date/time related objects (e.g. LocalDate or ZonedDateTime)

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

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.

Java 8 LocalDateTime

From the JAVA Oracle docs,

  • static LocalDateTime parse(CharSequence text)
    Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.

  • static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
    Obtains an instance of LocalDateTime from a text string using a specific formatter.

DateTimeFormatter object needs to be formatted.



Related Topics



Leave a reply



Submit