Read Also: [Solved] unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.
Let's dive deep into the topic:
[Fixed] Unable to Obtain LocalDateTime from TemporalAccessor
As always, first, we will produce the unable to obtain LocalDateTime from TemporalAccessor error before moving on to the solution.1. Producing the error by converting String to LocalDateTime
We can easily produce the error by converting String to LocalDateTime and the formatted string does not have time-related information as shown below in the example:
import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; public class TemporalAccessorError { public static void main(String args[]) { // Custom formatted date String String date = "2022/10/25"; // Creating DateTimeFormatter object with Specified format DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd"); // Converting String to LocalDateTimeLocalDateTime ldt = LocalDateTime.parse(date, dtf);// Print LocalDateTime instance System.out.println("LocalDateTime object is: " + ldt); } }
Output:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2022/10/25' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2022-10-25 of type java.time.format.Parsed
Explanation:
The formatted string does not contain time-related information. It contains only the Date information. To get rid of this error we need to call atStartOfDay() method after parsing the string. atStartOfDay() method will return the LocalDateTime object that will contain the time-related information too. In simple words, it will give you the LocalDateTime object at the specified date having the hour, minute, and second fields set to 0.
Solution:
As discussed above, we need to call the atStartOfDay() method on the parsed string as shown below in the example:
import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; import java.time.LocalDate; public class TemporalAccessorError { public static void main(String args[]) { // Given Custom formatted date String String date = "2022/10/25"; // Producing DateTimeFormatter object with Specified format DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd"); // Converting String to LocalDateTime objectLocalDateTime ldt = LocalDate.parse(date, dtf).atStartOfDay();// Display LocalDateTime instance System.out.println("LocalDateTime object is: " + ldt); } }
Output:
LocalDateTime object is: 2022-10-25T00:00
Solution 2: Using LocalDate instead of LocalDateTime
As we already knew, a formatted string does not contain time information, we may use LocalDate instead of LocalDateTime as shown below in the example:
import java.time.format.DateTimeFormatter; import java.time.LocalDate; public class TemporalAccessorError { public static void main(String args[]) { // Given Custom formatted date string String date = "2022/10/25"; // Creating DateTimeFormatter object with specified format DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd"); // Converting String to LocalDate objectLocalDate ld = LocalDate.parse(date, dtf);// Print LocalDate instance System.out.println("LocalDate object is: " + ld); } }
Output:
LocalDate object is: 2022-10-25
That's all for today. Please mention in the comments if you have any questions related to how to solve unable to obtain LocalDateTime from TemporalAccessor in Java.