-1

I'm trying to convert String to LocalDateTime object, but java is not letting me.

String date = in.nextLine().trim();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);

output:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2018-03-03' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2018-03-03 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalDateTime.parse(Unknown Source)

Thank you!

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • What would have been your expected result? Asking because with my understanding, what you are trying doesn’t really make good sense. Also what did your search and research turn up? Asking because I think that finding the same question asked before wouldn’t have been too hard. – Ole V.V. May 14 '18 at 17:50

1 Answers1

2

You are trying to parse a raw date to a LocalDateTime object which is not allowed.

You need to parse it to a LocalDate object instead.

Ben
  • 1,665
  • 1
  • 11
  • 22
  • Thank you very much @Ben !!! I m begginer. I did not know:) – Mikel Mikel May 14 '18 at 15:01
  • You're welcome. It's a weird specification that it's not allowed to instantiate a LocalDateTime without a Time although it does make sense partially. There could definitely be a better error message for that though. – Ben May 14 '18 at 15:04
  • Agree, it would be much easier to understand – Mikel Mikel May 14 '18 at 15:11