0

I try to user LocalDateTime and DateTimeFormatter and try to parse this String:

2020-04-28 and time: 12:37:49.863

But throws DateTimeParseException

This is code fragment:

String line = "2020-04-28 and time: 12:37:49.863";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 'and time: 'hh:mm:ss.SSS");
LocalDateTime localDateTime = LocalDateTime.parse(line, formatter);

Why does it occours?

Roberto
  • 1,288
  • 5
  • 23
  • 47
  • 2
    `HH` instead of `hh` maybe? – Dawood ibn Kareem Apr 27 '20 at 21:56
  • What is the exception message? Also are you sure `'and time: '` is supposed to be in there? How does the formatter know it is supposed to be taken literally? – smac89 Apr 27 '20 at 21:57
  • @smac89 so, before this, I successfully used this formatter to format LocalDateTime value. So, this is correct and time usage in pattern. So, this pattern works for write value, but doesn't work for parse value.. – Roberto Apr 27 '20 at 22:07
  • 2
    If you want to use `hh` instead of `HH`, I think you have to also use `a` for AM or PM - `"2020-04-28 and time: 12:37:49.863 AM"` and `"yyyy-MM-dd 'and time:' hh:mm:ss.SSS a"`. – andrewJames Apr 27 '20 at 22:08
  • @andrewjames yes, you are correct, I didn't know about this difference, thanks – Roberto Apr 27 '20 at 22:10
  • @DawoodibnKareem thanks – Roberto Apr 27 '20 at 22:10
  • 2
    @smac89 The single quotes tell the formatter that `and time: ` is literal text, not format pattern letters. Good question. – Ole V.V. Apr 28 '20 at 17:04

0 Answers0