Java 8 LocalDateTime dropping 00 seconds value when parsing date string value with 00 seconds like "2018-07-06 00:00:00"
Feature, not a bug
You are seeing the documented behavior of the particular DateTimeFormatter
used by the LocalDateTime::toString
method.
Excerpt, my emphasis:
The output will be one of the following ISO-8601 formats:
uuuu-MM-dd'T'HH:mm
uuuu-MM-dd'T'HH:mm:ss
uuuu-MM-dd'T'HH:mm:ss.SSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
If you want other behavior when generating a String to represent the value of you LocalDateTime
, use a different DateTimeFormatter
and pass it to LocalDateTime::format
.
String output = myLocalDateTime.format( someOtherFormatter ) ;
The LocalDateTime
has no “format” as it is not text. It is the job of the DateTimeFormatter
to parse or generate String
objects of a particular format.
You need to format your date with the proper formatter instead of using default one by calling toString()
.
final String lexicalDate = localDateTime.format(dateTimeFormatter);