jackson与SimpleDateFormat
所属分类 java
浏览量 993
SimpleDateFormat 非线程安全
为何 全局唯一的实例 ObjectMapper ,可以设置一个 SimpleDateFormat 实例
private static final ObjectMapper mapper = new ObjectMapper();
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
static{
//
// public class SimpleDateFormat extends DateFormat
mapper.setDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT,Locale.US));
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
com.fasterxml.jackson.databind.SerializerProvider._dateFormat()
protected final DateFormat _dateFormat()
{
if (_dateFormat != null) {
return _dateFormat;
}
// At this point, all timezone configuration should have occurred,
// with respect to default dateformat configuration.
// But we still better clone an instance as formatters are stateful, not thread-safe.
DateFormat df = _config.getDateFormat();
_dateFormat = df = (DateFormat) df.clone();
// [databind#939]: 26-Sep-2015, tatu: With 2.6, formatter has been (pre)configured
// with TimeZone, so we should NOT try overriding it unlike with earlier versions
//
// TimeZone tz = getTimeZone();
// if (tz != df.getTimeZone()) {
// df.setTimeZone(tz);
// }
return df;
}
java.text.SimpleDateFormat.clone()
@Override
public Object clone() {
SimpleDateFormat other = (SimpleDateFormat) super.clone();
other.formatData = (DateFormatSymbols) formatData.clone();
return other;
}
SimpleDateFormat.clone 上打断点
序列化反序列化时间字段时 ,每次都会clone一个实例
Thread [main] (Suspended (breakpoint at line 2324 in SimpleDateFormat))
SimpleDateFormat.clone() line: 2324
DefaultSerializerProvider$Impl(SerializerProvider)._dateFormat() line: 1430
DefaultSerializerProvider$Impl(SerializerProvider).defaultSerializeDateValue(Date, JsonGenerator) line: 1090
DateSerializer(DateTimeSerializerBase)._serializeAsString(Date, JsonGenerator, SerializerProvider) line: 212
DateSerializer.serialize(Date, JsonGenerator, SerializerProvider) line: 51
DateSerializer.serialize(Object, JsonGenerator, SerializerProvider) line: 15
BeanPropertyWriter.serializeAsField(Object, JsonGenerator, SerializerProvider) line: 727
BeanSerializer(BeanSerializerBase).serializeFields(Object, JsonGenerator, SerializerProvider) line: 719
BeanSerializer.serialize(Object, JsonGenerator, SerializerProvider) line: 155
DefaultSerializerProvider$Impl(DefaultSerializerProvider)._serialize(JsonGenerator, Object, JsonSerializer
完整测试代码
https://gitee.com/dyyx/springboothello/blob/master/src/test/java/dyyx/JacksonTest.java
时间格式化性能对比
SimpleDateFormat并发问题实例演示
上一篇
下一篇
中国历史上的两个短命王朝
JDK默认GC
时间格式化性能对比
中国简史
网络流行语
linux 获取进程工作目录