javadatetimejava-7simpledateformatdatetime-parsing

SimpleDateFormat with Offset not recognized


I needed to parse below Date coming from a web service.

2014-09-16T18:05:00.000-05:00

So I tried to created SimpleDateFormat object

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

JavaDocs has below example and format given in the table that matches with my date format.

"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"            2001-07-04T12:08:56.235-07:00

However, I am getting below exception. I use jdk 1.7.0_55. Is there something I am missing ?

Caused by: java.lang.IllegalArgumentException: Illegal pattern character 'X'
    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:768)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:575)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:500)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:475)

Solution

  • Check the version of Java you're using. I bet you're actually using 6.

    $ java -version
    java version "1.6.0_65"
    

    The XXX format was added in 7. Compare:

    http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

    http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html