Table of Contents
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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:=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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: