0

I tried this way but it throws an exception.

DateFormat date = new SimpleDateFormat("YYYY-MM-DDThh:mm:ssZ");
Date result =  date.parse(time);

I am getting this exception:

java.text.ParseException: Unparseable date: "2018-06-19T05:50:31.000+0000"

How can I parse this date in Java?

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Related: [Converting ISO 8601-compliant String to java.util.Date](https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) – Ole V.V. Jun 20 '18 at 11:09
  • 2
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also 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. Jun 20 '18 at 11:09
  • @OleV.V. agreed, though I've answered per the OP's use of simple date format. –  Jun 20 '18 at 11:13
  • 1
    @mrblewog maybe also add an example how to do it with the "new" api so OP can learn and others who stumble here using the simpledate library. – Tschallacka Jun 20 '18 at 11:14
  • The format pattern letters are case sensitive. You need lowercase `yyyy`, lowercase `dd` and uppercase `HH`. – Ole V.V. Jun 20 '18 at 11:15
  • In any case [my answer here](https://stackoverflow.com/a/48670652/5772882) shows how to do it with `java.time` (the 4 years old API; maybe still “new” compared to the approx. 25 years old `SimpleDateFormat`?). – Ole V.V. Jun 20 '18 at 11:44
  • For modern solution, see: [*Java 8 Date and Time: parse ISO 8601 string without colon in offset*](https://stackoverflow.com/q/46487403/642706) – Basil Bourque Jul 06 '19 at 06:05

1 Answers1

3

I think you need to make the T literal with single quotes, also the zone is a Z.

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

See the examples here: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html