[Fixed] Unable to obtain LocalDateTime from TemporalAccessor

Unable to obtain LocalDateTime from TemporalAccessor

In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8.

Unable to obtain LocalDateTime from TemporalAccessor : Reason

You will generally get this error, when you try to convert String to LocalDateTime and Formatted String does not have time related information.

Let’s understand with the help of example:

Output:=

Unable to obtain LocalDateTime from TemporalAccessor : Fix

LocalDate’s parse() method with atStartOfDay()

As Formatted String does not contain time information, we need to use LocalDate’s parse() method and call atStartOfDay() method to get LocalDateTime object.

Output:

LocalDateTime obj: 2022-02-16T00:00

Use LocalDate instead of LocalDateTime

Since formatted String does not contain time information, we may want to use LocalDate rather than LocalDateTime.

We can use LocalDate’s parse() method to convert String to LocalDate in Java.

Output:

LocalDate obj: 2022-02-16

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *