Search code examples
javadatetimeexceptionzoneddatetime

java ZonedDateTime parse error for milliseconds part


ZonedDateTime zdt3 = ZonedDateTime.parse("1999-09-09 09:09:09.999", 
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.xxx"));

Runtime error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '1999-09-09 09:09:09.999' could not be parsed at index 20

How to fix my case?


Solution

  • See the doc of ZonedDateTime:

    A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

    1999-09-09 09:09:09.999 does not hold any zone information, it could be a LocalDateTime:

    LocalDateTime zdt3 = LocalDateTime.parse("1999-09-09 09:09:09.999",
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));