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

拈花ヽ惹草 提交于 2019-12-11 01:29:57

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/29759653/jackon-2-4-2-failing-to-deserialize-valid-date-even-after-specifying-date-forma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!