javadatetimejava-8java-timedatetime-parsing

Java 8 Date and Time API - parse yyyy-MM-dd'T'HH:mm:ss.SSSZ


I'm trying to parse date in ISO8601 format:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

Am I correct that it is not possible to parse it with any of the default formats defined in java.time.format.DateTimeFormatter?

For example ISO_OFFSET_DATE_TIME will parse only:

yyyy-MM-dd'T'HH:mm:ss.SSSZZ

Samples:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
2015-04-29T10:15:00.500+0000

yyyy-MM-dd'T'HH:mm:ss.SSSZZ
2015-04-29T10:15:00.500+00:00

BTW:I know I can define my own formatter that is not the issue. Just wanted to ensure that I'm not missing something as the ISODateTimeFormat of Joda is able to parse both:

 org.joda.time.format.DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
 DateTime dateTime = dateTimeFormatter.parseDateTime("2015-04-29T10:15:00.500+0000");

Solution

  • You should never be bothered by those annoying date-format.

    There has an new library dateparser.

    It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly.

    With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd'T'HH:mm:ss.SSSZZ:

    Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
    Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
    LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");
    

    All works fine, please enjoy it.