-2

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?

Aikas
  • 67
  • 2
  • 7
  • 7
    What do you think a `LocalDate` represents? – Sotirios Delimanolis Jun 10 '17 at 14:58
  • 4
    As the name suggests LocalDate just stores year, month and day . LocalDateTime is the one to go for if you want both date and time. – Pallavi Sonal Jun 10 '17 at 14:59
  • 5
    Also, remove the trailing space at the end of the pattern. – JB Nizet Jun 10 '17 at 15:02
  • 2
    It is a strength, though also a little bit of an obstacle, that there are several modern Java date and time classes. You need to make a little effort to pick the right one for your purpose. Once you’ve done that, you have a classes that is (usually) exactly fitted for that purpose rather than the old supposed-to-fit-all `Date` class. @PallaviSonal is correct about which class you need in this case. – Ole V.V. Jun 10 '17 at 15:09

1 Answers1

9

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"));
bowmore
  • 10,842
  • 1
  • 35
  • 43
  • now , between date and time i get a 'T' is posible to remove? @bowmore – Aikas Jun 10 '17 at 15:08
  • 1
    @Aikas The string with T is the result of `toString` method. If you want a different format, use a `DateTimeFormatter` –  Jun 10 '17 at 15:10
  • That's the default formatting you get when printing a `LocalDateTime`, use `DateTimeFormatter` to print it in the format you want. – bowmore Jun 10 '17 at 15:10
  • @Andreas the T is not needed in the `DateTimeFormatter` used for parsing. In fact it will make parsing fail, as the source Strings don't have a 'T'. – bowmore Jun 10 '17 at 15:12