convert String "yyyy-MM-dd" to LocalDateTime
Use LocalDate
to create a localDate and then you can add the timepart if you need it:
DateTimeFormatter DATEFORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate ld = LocalDate.parse("2017-03-13", DATEFORMATTER);
LocalDateTime ldt = LocalDateTime.of(ld, LocalDateTime.now().toLocalTime());
System.out.println(ldt);
or LocalDateTime
ldt = LocalDateTime.of(ld, LocalDateTime.MIN.toLocalTime());
if you just need an empty timepart
EDIT:
Look at this solution with this you can build your dynamic parser:
DateTimeFormatter DATEFORMATTER1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter DATEFORMATTER = new DateTimeFormatterBuilder().append(DATEFORMATTER1)
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter();
//DateTimeFormatter DATEFORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime ldt = LocalDateTime.parse("2017-03-13", DATEFORMATTER);
You can not convert "2017-03-13" to a LocalDateTime
since there is no time information in the string, only date. You can convert it to a LocalDate
DateTimeFormatter dateformatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate ld = LocalDate.parse("2017-03-13", dateformatter);
after this we can covert it to LocalDateTime
LocalDateTime ldt = ld.atStartOfDay();