Search code examples
javatimekmldatetime-parsingjava-time

Java Instant.parse on Date java 8


I have some legacy KML documents which includes a time stamp entry. Why is the below date not valid when using Instant to parse? Both methods are suppose to parse ISO 8601 formatted dates.

String dateString = "2017-12-04T08:06:60Z"

Using

java.time.Instant.parse(dateString)

throws an error

"DateTimeParseException Text 2017-12-04T08:06:60Z could not be parsed at index 0."

However, when using

Date myDate =   javax.xml.bind.DatatypeConverter.parseDateTime( dateString )

myDate is parsed correctly....


Solution

    1. 60 seconds isn't a valid time. Meaning that this is invalid 2017-12-04T08:06:60Z, if it was 60 seconds then the minute should have incremented and your time would be 2017-12-04T08:07:00Z
    2. Using a valid date and then parsing the String would work just fine:

      String date = "2017-12-04T08:07:00Z";
      System.out.println(Instant.parse(date));
      

    Also java.time ignores leap seconds. From the docs:

    Implementations of the Java time-scale using the JSR-310 API are not required to provide any clock that is sub-second accurate, or that progresses monotonically or smoothly. Implementations are therefore not required to actually perform the UTC-SLS slew or to otherwise be aware of leap seconds. JSR-310 does, however, require that implementations must document the approach they use when defining a clock representing the current instant. See Clock for details on the available clocks.