timezone-offsetdatetime-parsingjs-joda

How do you parse basic (short/compact) ISO:8601 string with Joda?


I have a time string, which looks like this: 20170822T194135+00. This is called basic ISO:8601 format, if I understood correctly.

When I try to parse it using ZonedDateTime, it throws exception, complaining that it can't parse it.

SO, how do I convert this string to a valid Joda datetime object?

Do I need to build manual "format" to parse it (this would be silly, considering it's a standard format)?


Desperately, I've actually tried to implement custom format:

const time = ZonedDateTime.parse(timeString, DateTimeFormatter.ofPattern(`yyyyMMdd'T'HHmmssZ`));

But, it throws error on column 15. Looks like it fails to parse the timezone. Is my implementation correct? How do I make it work?


Solution

  • I could do it using the pattern x as described in the docs (this pattern accepts offsets like +00).

    Then I parsed directly to a ZonedDateTime:

    const formatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssx");
    
    const time = ZonedDateTime.parse("20170822T194135+00", formatter);
    

    The resulting time variable has the value equivalent to 2017-08-22T19:41:35Z.

    The built-in formatters (such as ISO_LOCAL_DATE_TIME) can't parse this format, so the only way seems to be creating a formatter.