Jackon 2.4.2即使在指定日期格式后也无法反序列出有效日期 [英] Jackon 2.4.2 failing to deserialize valid date, even after specifying date format
问题描述
SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy- MM-dd'T'HH:mm:ssZ);
dateFormat.setTimeZone(TimeZone.getTimeZone(UTC));
mapper.setDateFormat(dateFormat);
然后我尝试使用该映射器对以下JSON进行反序列化:
{
id:11,
已确认:false,
creationDate:2015-04-20T22 :27:41Z,
lastUpdateDate:2015-04-20T22:27:41Z,
name:test
}
使用以下行:
test = mapper.readValue(jsonString,Test.class);
,它失败:
com.fasterxml.jackson.databind.exc.InvalidFormatException:不能从String值'2015-04-20T22:27:41Z'构造java.util.Date的实例:不是有效的表示(错误:无法解析日期值'2015-04-20T22:27:41Z':不可稀释日期:2015-04-20T22:27:41Z)
任何想法我做错了什么?我似乎无法弄清楚我失踪了什么...
谢谢!
使用 yyyy-MM-dd'THH:mm:ssX
。 Z
用于RFC 822时区,Z不是这样的时区。 X
用于ISO 8601时区,Z是这样的时区。
https://docs.oracle.com/javase/7/docs/api/java /text/SimpleDateFormat.html
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!
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
这篇关于Jackon 2.4.2即使在指定日期格式后也无法反序列出有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!