Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8) Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8) java java

Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)


It turns out Java does not accept a bare Date value as DateTime. Using LocalDate instead of LocalDateTime solves the issue:

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


If you really need to transform a date to a LocalDateTime object, you could use the LocalDate.atStartOfDay(). This will give you a LocalDateTime object at the specified date, having the hour, minute and second fields set to 0:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");LocalDateTime time = LocalDate.parse("20140218", formatter).atStartOfDay();


For what is worth if anyone should read again this topic(like me) the correct answer would be in DateTimeFormatter definition, e.g.:

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(); 

One should set the optional fields if they will appear. And the rest of code should be exactly the same.