2

am using spring boot and Java 1.8 in my project. We are receiving a date string from an outside system which we need to parse. The problem is the outside system can send any DateTime that is ISO 8601 compliant and we need to parse any ISO 8601 format string that comes. Can anyone suggest me how to do this? is there any library for doing this?

Two DateTime formats passed are 2018-11-01T16:26:15+0100, 2018-10-31T08:27:00.0000000Z and there can be many more.

I have found some posts on StackOverflow that suggest to use Joda Time converter, but I am unable to parse the date 2018-10-31T08:27:00.0000000Z with that.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Anirban
  • 925
  • 4
  • 24
  • 54
  • 1
    You should link [the other SO post](https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) or people are just going to vote to close as a duplicate. – Bacon Bits Nov 05 '18 at 13:21
  • Sorry, it is 2018-10-31T08:27:00.0000000Z – Anirban Nov 05 '18 at 13:31
  • `DateTimeFormatter.ISO_DATE_TIME.parse(yourString)` – Holger Nov 05 '18 at 13:32
  • In which package is this available? In the Joda DateTime package? – Anirban Nov 05 '18 at 13:39
  • 4
    Since you tagged your question with `[java-8]`, you should go for the standard API, [`java.time.format`](https://docs.oracle.com/javase/8/docs/api/?java/time/format/package-summary.html) – Holger Nov 05 '18 at 13:53
  • DateTimeFormatter.ISO_DATE_TIME.parse("2018-11-01T16:26:15+0100"); returns a type TemporalAccessor, how to get a Date object from it? – Anirban Nov 05 '18 at 14:32
  • It was unable to parse the String 2018-11-01T16:26:15+0100 – Anirban Nov 05 '18 at 14:50
  • Please take a look at: https://stackoverflow.com/questions/29956175/json-java-8-localdatetime-format-in-spring-boot – Chlebik Nov 05 '18 at 15:35
  • Possible duplicate of [Converting ISO 8601-compliant String to java.util.Date](https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date). With Java 8 use [this answer](https://stackoverflow.com/a/27479533/5772882) and [this one](https://stackoverflow.com/a/20578792/5772882) (and also with other Java versions). – Ole V.V. Nov 06 '18 at 04:46
  • 1
    I suggest you complain to the authors of that external service. Returning values in a wide range of ISO 8601 formats is unreasonable, and contradicts the very purpose of ISO 8601: to ease the exchange of date-time values textually. – Basil Bourque Nov 07 '18 at 16:37

1 Answers1

5

This may solve it for you or at least be a start:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
            .appendPattern("XX")
            .toFormatter();

    String[] stringsPassed = {
            "2018-11-01T16:26:15+0100",
            "2018-10-31T08:27:00.0000000Z"
    };
    
    for (String sample : stringsPassed) {
        OffsetDateTime odt = OffsetDateTime.parse(sample, formatter);
        System.out.println(odt);
    }

Output:

2018-11-01T16:26:15+01:00
2018-10-31T08:27Z

It doesn’t parse all thinkable ISO 8601 strings, but may parse those you can get. Since you have only shown us two samples, I cannot know.

java.time, the modern Java date and time API, is quite ISO 8601 friendly. It handles presence and absence of seconds and fraction of second (up to 9 decimals). This is what I am using DateTimeFormatter.ISO_LOCAL_DATE_TIME for in the code. The slight issue is that the built-in DateTimeFormatter.ISO_OFFSET_DATE_TIME, which would otherwise seem right here, requires a colon in the UTC offset, as in +01:00. Instead I am using format pattern XX. It accepts an offset without colon and also accepts Z as in your second example. If still more flexibility required, you may look into optional parts in the formatter. Check the documentation.

Joda-Time? Not recommended when you are using Java 8. A couple of quotes from the Joda-Time home page:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

Even on Java 6 and 7 I would have recommended the backport of java.time over Joda-Time.

Links

Lisa
  • 4,333
  • 2
  • 27
  • 34
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161