jackson版本1.6.1
问题描述如下:序列化时间是比实际时间少8小时
public class JacksonTest {
public static void main(String[] args){
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
ObjectMapper mapper2 = new ObjectMapper();
mapper2.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"));
StringWriter sw = new StringWriter();
StringWriter sw1 = new StringWriter();
JsonGenerator gen;
JsonGenerator gen1;
try {
gen = new JsonFactory().createGenerator(sw);
gen1 = new JsonFactory().createGenerator(sw1);
mapper2.writeValue(gen, date);
mapper2.writeValue(gen1, timestamp);
gen.close();
} catch (IOException e) {
} finally {
}
System.out.println(timestamp);
System.out.println(sw.toString());
System.out.println(sw1.toString());
}
}
输出:
2015-12-03 19:01:40.983
"2015-12-03 11:01:40 +0000"
"2015-12-03 11:01:40 +0000"
问题原因:
jackson在序列化时间时是按照国际标准时间GMT进行格式化的,而在国内默认时区使用的是CST时区,两者相差8小时,
经调试,这应该属于Jackson的bug,如下是ObjectMapper源码
/**
* Base settings contain defaults used for all {@link ObjectMapper}
* instances.
*/
protected final static BaseSettings DEFAULT_BASE = new BaseSettings(
null, // can not share global ClassIntrospector any more (2.5+)
DEFAULT_ANNOTATION_INTROSPECTOR,
STD_VISIBILITY_CHECKER, null, TypeFactory.defaultInstance(),
null, StdDateFormat.instance, null,
Locale.getDefault(),
// TimeZone.getDefault()
TimeZone.getTimeZone("GMT"),
Base64Variants.getDefaultVariant() // 2.1
);
jackson没有去默认的时区,而是取GMT时区。
解决办法:重写ObjectMapper类,包路径和jackson的包路径相同。