Reputation: 543
The code (Java 8) snippet below drops the seconds part of my date time when the seconds value is zero within the date parsed using LocalDateTime.parse, like 2018-07-10 00:00:00:
final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final LocalDateTime localDateTime = LocalDateTime.parse("2018-07-06 00:00:00", dateTimeFormatter);
final String lexicalDate = localDateTime.toString();
System.out.println("Lexical Date : "+ lexicalDate);
final XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate);
System.out.println("Gregorian Calendar : "+ gregorianCalendar);
The Lexical Date is printed as :
Lexical Date : 2018-07-10T00:00
instead of :
Lexical Date : 2018-07-10T00:00:00
Now this is affecting the date value of the gregorian calendar which returns null when the second is dropped. Other cases when the seconds value is greater than Zero, it works perfectly.
javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate)
The above code is returning null whenever the seconds value is dropped because of 00 seconds value within the parsed string.
Can someone assist with a better way that handles this issue using LocalDate time, otherwise it might be a bug/funny control in Java 8 LocalDateTime.
Please note I do not have control over this date value, it's coming from a third party platform.
Upvotes: 30
Views: 23180
Reputation: 51
DateTimeFormatter removes 00 from date after parsing, you just have to reformat it again
please refer below code snippet:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String currentTime= "2017-10-19 22:00:00";
LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
//2017-10-19T22:00
String formattedDate=datetime.format(formatter);
//2017-10-19 22:00:00
Upvotes: 5
Reputation: 431
You can use this pattern bellow in order to achieve the desired format:
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
Upvotes: 2
Reputation: 338496
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.
Upvotes: 29
Reputation: 1957
You need to format your date with the proper formatter instead of using default one by calling toString()
.
final String lexicalDate = localDateTime.format(dateTimeFormatter);
Upvotes: 4