Search code examples
javadatejava-timedate-parsinglocaldate

localdate parsing can't parse hours and minutes


I need to get the date from a String, the format is the following: uuuu-MM-dd HH:mm

The input has that format and I want the same format for my dates (year-month-day hour:minutes). When I did this:

LocalDate aux = LocalDate.parse(times.get(index),DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm "));

with this input: 2017-12-05 20:16 I get only this: 2017-12-05

I don't know why, I've tried a lot of formats and always lost the hours and minutes. Any idea?


Solution

  • LocalDate only has year, month, day fields. It cannot contain time of day data.

    Use LocalDateTime instead.

    LocalDateTime aux = LocalDateTime.parse("2017-12-05 20:16", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"));