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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…