2

I am trying to convert the below String to LocalDateTime, but somehow its not working.

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.SSS");
val dateTime = LocalDateTime.parse("2016-09-21 13:43:27.000", formatter);

It seems there is a problem in Pattern.

Shankar
  • 8,529
  • 26
  • 90
  • 159
  • This isn't really a duplicate of the nominated question. The nominated question wants java.util.Date, and this wants java.time.LocalDateTime. – dcsohl Aug 09 '17 at 15:59

1 Answers1

7

You have a problem with your pattern. You should use 'H' for hours (0-23). The type for formatter and dateTime is wrong.

This would work correctly:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.parse("2016-09-21 13:43:27.000", formatter);
informatik01
  • 16,038
  • 10
  • 74
  • 104
David SN
  • 3,389
  • 1
  • 17
  • 21