Failed to get LocalDateTime from TemporalAccessor while parsing LocalDateTime (Java 8)

I am just trying to convert a date string to a DateTime object in Java 8. After running the following lines:

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

I get the following error:

 Exception in thread "main" java.time.format.DateTimeParseException: Text '20140218' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2014-02-18 of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853) at java.time.LocalDateTime.parse(LocalDateTime.java:492) 

The syntax is identical to what was suggested here , but I am served with an exception. I am using JDK-8u25 .

+86
java datetime java-8 datetime-format
Dec 13
source share
9 answers

Turns out Java doesn't take the โ€œBare Dateโ€ value as a DateTime. Using LocalDate instead of LocalDateTime solves the problem:

 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate dt = LocalDate.parse("20140218", formatter); 
+109
Dec 13 '14 at 0:20
source share

If you really need to convert the date to a LocalDateTime object, you can use LocalDate.atStartOfDay (). This will give you a LocalDateTime object on the specified date, with the hour, minute and second field 0 set to:

 final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDateTime time = LocalDate.parse("20140218", formatter).atStartOfDay(); 
+43
01 Oct '15 at 11:33
source share

For what it's worth, if someone needs to read this topic again (like me), the correct answer would be in a DateTimeFormatter definition, for example:

 private static DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder().appendPattern("dd/MM/yyyy[ [HH][:mm][:ss][.SSS]]") .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(); 

You must specify optional fields if they appear. The rest of the code should be exactly the same.

+30
Sep 25 '16 at 9:11
source share

This is a really obscure and useless error message. After much trial and error, I found that LocalDateTime will LocalDateTime the above error if you don't try to parse the time. Using LocalDate instead, it works without errors.

This is poorly documented, and the corresponding exception is very useless.

+28
Jan 26 '15 at 23:33
source share

Extension to answer retrogrades ..: I had the same problem even when using LocalDate rather than LocalDateTime . The problem was that I created a DateTimeFormatter using .withResolverStyle(ResolverStyle.STRICT); so I had to use the uuuuMMdd date uuuuMMdd instead of yyyyMMdd (i.e. "Year" instead of "year-era")!

 DateTimeFormatter formatter = new DateTimeFormatterBuilder() .parseStrict() .appendPattern("uuuuMMdd") .toFormatter() .withResolverStyle(ResolverStyle.STRICT); LocalDate dt = LocalDate.parse("20140218", formatter); 

(This solution was originally a response to the retrographs answer, but I was asked to post it as a standalone answer, because it seems to work great for many people.)

+16
Apr 28 '17 at 8:10
source share

For those who have landed here with this error, like me:

Unable to obtain LocalDateTime from TemporalAccessor: {HourOfAmPm=0, MinuteOfHour=0}

It came from the following line:

 LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy h:mm")); 

It turned out that this was because I used a 12hr Hour pattern in 0 hour instead of a 24-hour picture.

Changing the hour to a 24-hour picture using capital H captures this:

 LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy H:mm")); 
+10
May 17 '18 at 19:18
source share

If the String date does not contain the values โ€‹โ€‹of hours, minutes, etc., you cannot directly convert this to LocalDateTime . You can convert it only to LocalDate , because the line represents only the components of the year, month and date, this will be the right thing.

 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate ld = LocalDate.parse("20180306", dtf); // 2018-03-06 

In any case, you can convert this to LocalDateTime .

 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate ld = LocalDate.parse("20180306", dtf); LocalDateTime ldt = LocalDateTime.of(ld, LocalTime.of(0,0)); // 2018-03-06T00:00 
+9
Mar 12 '18 at 9:38
source share

Try this:

 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy"); LocalDate fromLocalDate = LocalDate.parse(fromdstrong textate, dateTimeFormatter); 

You can add any format you want. This is suitable for me!

+1
Nov 07 '18 at 21:09
source share

It works great

 public class DateDemo { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm"); String date = "16-08-2018 12:10"; LocalDate localDate = LocalDate.parse(date, formatter); System.out.println("VALUE="+localDate); DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm"); LocalDateTime parse = LocalDateTime.parse(date, formatter1); System.out.println("VALUE1="+parse); } } 

exit:

 VALUE=2018-08-16 VALUE1=2018-08-16T12:10 
0
Dec 04 '18 at 15:21
source share



All Articles