Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 - Trying to convert String to LocalDateTime [duplicate]

Tags:

date

java-8

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.

like image 964
Shankar Avatar asked Dec 06 '25 13:12

Shankar


1 Answers

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);
like image 184
David SN Avatar answered Dec 10 '25 00:12

David SN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!