Edy Bourne
Edy Bourne

Reputation: 6187

Jackon 2.4.2 failing to deserialize valid date, even after specifying date format

I am attempting to deserialize a JSON with a custom date format. It is failing, even though I have set a date format on the object mapper:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    mapper.setDateFormat(dateFormat);

Then I attempt to deserialize the following JSON using that mapper:

{
  "id": 11,
  "confirmed": false,
  "creationDate": "2015-04-20T22:27:41Z",
  "lastUpdateDate": "2015-04-20T22:27:41Z",
  "name": "test"
}

Using the line:

Test test = mapper.readValue(jsonString, Test.class);

and it fails with:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-04-20T22:27:41Z': not a valid representation (error: Failed to parse Date value '2015-04-20T22:27:41Z': Unparseable date: "2015-04-20T22:27:41Z")

Any ideas what am I doing wrong? I can't seem to figure out what I am missing...

Thank you!

Upvotes: 2

Views: 2064

Answers (1)

JB Nizet
JB Nizet

Reputation: 691725

Use yyyy-MM-dd'T'HH:mm:ssX. Z is for an RFC 822 time zone, and Z is not such a timezone. X is for a ISO 8601 time zone, and Z is such a timezone.

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

Upvotes: 3

Related Questions