-1

I have a date in the following format:

2017-04-09T11:15:39.200+03:00

I used the following format string:

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

But i am getting an exception"

java.text.ParseException: Unparseable date: "2017-04-09T11:15:39.200+03:00"

Thanks

john paul
  • 1
  • 2
  • 4
    Why did you put ‘Z’ at the end of your pattern? That means your date time String has to end in the character Z and clearly it doesn’t. – Erwin Bolwidt Apr 09 '18 at 13:30
  • remove 'z' it's useless – Basil Battikhi Apr 09 '18 at 13:31
  • 1
    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/). Furthermore the modern `OffsetDateTime` class will parse your string without any explicit formatter. It couldn’t be easier. – Ole V.V. Apr 09 '18 at 14:42
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 09 '18 at 22:57

2 Answers2

1

What's wrong is that you didn't read the documentation: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

You'd see that the pattern to use is X:

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

And you also didn't check in stackoverflow before posting: in the same tags you used (https://stackoverflow.com/questions/tagged/simpledateformat), there's another question - a very recent one, asked today - with basically the same problem (using 'Z' inside quotes):

Gson: java.text.ParseException: Unparseable date: "2018-04-09T09:00:00+02:00"

well
  • 85
  • 4
0

The API docs for what you say you want shows this as the for-matter for your output wish.

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html