0

Try 1:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+/-HHmm");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);

Try 2:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);

Try 3:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = format.parse(createdDate2);

Nothing seems to work with this format:

Any help?

Nagendra Singh
  • 577
  • 1
  • 7
  • 24
  • Any particular reason why you are using the `SimpleDateFormat`, `TimeZone` and `Date` classes? Those are all long outdated, and `SimpleDateFormat` in particular notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 11 '18 at 19:35
  • guess, why Oracle has not deprecated Date object completely? because there is still lots of code using Date Objects... – Levancho Apr 11 '20 at 22:04

2 Answers2

2
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = format.parse(createdDate2);
Ralf Renz
  • 1,061
  • 5
  • 7
1

This might not exactly be what you want, but if the timezone offset would be written with a colon separator e.g. +00:00 it's the ISO_OFFSET_DATE_TIME

OffsetDateTime d = OffsetDateTime.parse("2018-07-03T01:00:21.000+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(d); // 2018-07-03T01:00:21Z
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Good suggestion. To parse `+0000` as an offset, you may build a `DatetimeFormatter` using a format pattern string with `xx` for offset. – Ole V.V. Jul 11 '18 at 19:39
  • When I try to parse this String as CharacterSequence : '2018-07-10T01:00:03.000+0000' I get this error: `java.time.format.DateTimeParseException: Text '2018-07-10T01:00:03.000+0000' could not be parsed at index 23` – Nagendra Singh Jul 17 '18 at 07:19
  • @NagendraSingh That's because there is no colon in the timezone offset. – Karol Dowbecki Jul 17 '18 at 08:36