Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 LocalDateTime dropping 00 seconds value when parsing date string value with 00 seconds like "2018-07-06 00:00:00"

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.

like image 891
Taurai Nyamakura Avatar asked Jun 10 '18 17:06

Taurai Nyamakura


People also ask

How do I parse LocalDateTime?

In order to create a LocalDateTime object from a string, you can use the static LocalDateTime. parse() method. It takes a String and a DateTimeFormatter as a parameter. The DateTimeFormatter argument is used to specify the date/time pattern.

Is Java time LocalDateTime immutable?

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second.

What is the default format of LocalTime in Java 8?

LocalDateTime class, introduced in Java 8, represents a local date-time object without timezone information. The LocalDateTime class in Java is an immutable date-time object that represents a date in the yyyy-MM-dd-HH-mm-ss. zzz format. It implements the ChronoLocalDateTime interface and inherits the object class.


1 Answers

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.

like image 61
Basil Bourque Avatar answered Oct 12 '22 04:10

Basil Bourque