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

package org.arpit.java2blog;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class StringToLocalDateTime {

    public static void main(String[] args) {

        // Custom formatted String
        String dateStr = "2022-02-16";

        // Create DateTimeFormatter instance with specified format
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // Convert String to LocalDateTime using Parse() method
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr,dateTimeFormatter);

        // Print LocalDateTime object
        System.out.println("LocalDateTime obj: "+localDateTime);
    }
}

Output:=

Exception in thread "main" java.time.format.DateTimeParseException: Text '2022-02-16' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2022-02-16 of type java.time.format.Parsed
    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at org.arpit.java2blog.StringToLocalDateTime.main(StringToLocalDateTime.java:17)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2022-02-16 of type java.time.format.Parsed
    at java.base/java.time.LocalDateTime.from(LocalDateTime.java:461)
    at java.base/java.time.format.Parsed.query(Parsed.java:235)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2022-02-16 of type java.time.format.Parsed
    at java.base/java.time.LocalTime.from(LocalTime.java:431)
    at java.base/java.time.LocalDateTime.from(LocalDateTime.java:457)
    ... 4 more
    

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.

package org.arpit.java2blog;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class StringToLocalDateTime {

    public static void main(String[] args) {

        // Custom formatted String
        String dateStr = "2022-02-16";

        // Create DateTimeFormatter instance with specified format
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // Convert String to LocalDateTime using LocalDate's parse() method
        LocalDateTime localDateTime = LocalDate.parse(dateStr,dateTimeFormatter).atStartOfDay();

        // Print LocalDateTime object
        System.out.println("LocalDateTime obj: "+localDateTime);
    }
}

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.

package org.arpit.java2blog;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class StringToLocalDate {

    public static void main(String[] args) {
        // Custom formatted String
        String dateStr = "2022-02-16";

        // Create DateTimeFormatter instance with specified format
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // Convert String to LocalDateTime using LocalDate's parse() method
        LocalDate localDate = LocalDate.parse(dateStr,dateTimeFormatter);

        // Print LocalDateTime object
        System.out.println("LocalDate obj: "+localDate);
    }
}

Output:

LocalDate obj: 2022-02-16
[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.