1

Is 2021-07-27T09:58:51.000+0000 a right ISO 8601 format? Could not locate an online validator which claims to say if this is compliant or not.

Had issues while parsing this date below are the ways tried and the result is below:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

public class DateParser {
    public static void main(String[] args) {
        String s = "2021-07-27T09:58:51.000+0000";
        try {
            System.out.println("Success 001: " + OffsetDateTime.parse(s));
        } catch (DateTimeParseException e) {
            System.err.println("Fail: OffsetDateTime.parse: " + e.getMessage());
        }

        try {
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
            System.out.println("Success 002: " + df1.parse(s));
        } catch (ParseException e) {
            System.err.println("Fail: SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss.SSSZ: " + e.getMessage());
        }

        try {
            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            System.out.println("Success 003: " + df1.parse(s));
        } catch (ParseException e) {
            System.err.println("Fail: SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss.SSSXXX\"): " + e.getMessage());
        }

        try {
            TemporalAccessor ta = DateTimeFormatter.ISO_INSTANT.parse(s);
            Instant i = Instant.from(ta);
            System.out.println("Success 004: " + Date.from(i));
        } catch (DateTimeException e) {
            System.err.println("Fail: TemporalAccessor: " + e.getMessage());
        }

        try {
            System.out.println("Success 005: " + javax.xml.bind.DatatypeConverter.parseDateTime(s));
        } catch (IllegalArgumentException e) {
            System.err.println("Fail: javax.xml.bind.DatatypeConverter: " + e.getMessage());
        }

        try {
            System.out.println("Success 006: " + org.apache.commons.lang3.time.DateUtils.parseDate(s,
                    "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
        } catch (ParseException e) {
            System.err.println("Fail: org.apache.commons.lang3.time.DateUtils: " + e.getMessage());
        }

        try {
            System.out.println("Success 007: " + new org.joda.time.DateTime(s).toDate());
        } catch (IllegalArgumentException e) {
            System.err.println("Fail: org.joda.time.DateTime: " + e.getMessage());
        }
    }
}

Result

Fail: OffsetDateTime.parse: Text '2021-07-27T09:58:51.000+0000' could not be parsed at index 23
Success 002: Tue Jul 27 15:28:51 IST 2021
Fail: SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"): Unparseable date: "2021-07-27T09:58:51.000+0000"
Fail: TemporalAccessor: Text '2021-07-27T09:58:51.000+0000' could not be parsed at index 23
Fail: javax.xml.bind.DatatypeConverter: 2021-07-27T09:58:51.000+0000
Success 006: Tue Jul 27 15:28:51 IST 2021
Success 007: Tue Jul 27 15:28:51 IST 2021

From the above result, could you suggest a recommended way of parsing the above kind of date and any other ISO date string?

James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • Try `2021-07-27T09:58:51.000+00:00`, which I think is the valid ISO format. – Joachim Isaksson Oct 14 '21 at 04:54
  • Does this answer your question? [Why can't OffsetDateTime parse '2016-08-24T18:38:05.507+0000' in Java 8](https://stackoverflow.com/questions/39133828/why-cant-offsetdatetime-parse-2016-08-24t183805-5070000-in-java-8). Does [this](https://stackoverflow.com/questions/18823627/java-string-to-datetime), [this](https://stackoverflow.com/questions/43360852/cannot-parse-string-in-iso-8601-format-lacking-colon-in-offset-to-java-8-date), [this](https://stackoverflow.com/questions/48230408/parse-and-retrieve-timezone-offset-from-date-time)? – Ole V.V. Oct 14 '21 at 05:15
  • I’m unsure which original you find best. There’s also [this](https://stackoverflow.com/questions/48412345/convert-date-into-aest-using-java), [this](https://stackoverflow.com/questions/48666263/java-date-parsing-why-do-i-get-an-error), [this](https://stackoverflow.com/questions/53291240/java-to-mysql-i-need-convert-from-string-parametre-to-timestamp), [this](https://stackoverflow.com/questions/61269697/convert-a-string-to-zoneddatetime), [this](https://stackoverflow.com/questions/65844748/how-to-parse-zoneddatetime-with-milliseconds) and more. – Ole V.V. Oct 14 '21 at 05:19
  • @JoachimIsaksson The string in the question is valid according to [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) too. The colon in the offset is optional. – Ole V.V. Oct 14 '21 at 05:20
  • I unconditionally recommend parsing into an`OffsetDateTime` using the two-arg `OffsetDateTime.parse(CharSequence, DateTimeFormatter)` and a custom formatter like `new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE_TIME).appendOffset("+HHmmss", "+0000").toFormatter(Locale.ROOT)`. – Ole V.V. Oct 14 '21 at 05:59
  • [See the recommended way run live on IdeOne.com](https://ideone.com/KXbFCx). – Ole V.V. Oct 14 '21 at 06:08
  • 1
    @BobKuhar, very much the same problem what I am trying to solve – James Jithin Oct 14 '21 at 09:18

0 Answers0