- public final void defaultSerializeDateValue(long timestamp, JsonGenerator gen) throws IOException {
- if (this.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
- gen.writeNumber(timestamp);
- } else {
- gen.writeString(this._dateFormat().format(new Date(timestamp)));
- }
- }
- public final void defaultSerializeDateValue(Date date, JsonGenerator gen) throws IOException {
- if (this.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
- gen.writeNumber(date.getTime());
- } else {
- gen.writeString(this._dateFormat().format(date));
- }
- }
- public class Event {
- public String name;
- @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
- public Date eventDate;
- }
- public class CustomDateSerializer extends StdSerializer<Date> {
- //...
- }
- public class Event {
- public String name;
- @JsonSerialize(using = CustomDateSerializer.class)
- public Date eventDate;
- }
- private static ObjectMapper createObjectMapper() {
- ObjectMapper objectMapper = new ObjectMapper();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- objectMapper.setDateFormat(df);
- return objectMapper;
- }
@StaxMan: I am a bit concerned if ObjectMapper is still thread-safe after ObjectMapper#setDateFormat() is called. It is known that SimpleDateFormat is not thread safe, thus ObjectMapper won't be unless it clones e.g. SerializationConfig before each writeValue() (I doubt). Could you debunk my fear? – dma_k Aug 2, 2013 at 12:09
DateFormat is indeed cloned under the hood. Good suspicion there, but you are covered. – StaxMan Aug 2, 2013 at 19:43推荐阅读
- vivoX70pro和苹果11区别对比_哪款更值得入手
- 小米10s和小米10拍照哪个好_小米10s和小米10拍照对比
- vivox70pro+和小米11Ultra那个好_详细对比
- 潘婷秀发补水笔多少钱_潘婷秀发补水笔用多久
- i5 11300H和r5 5500u哪个好_i5 11300H和r5 5500u对比
- 天玑1200和骁龙888哪个发热严重_发热对比
- 华为mate40e和荣耀magic3对比_哪款更值得入手
- gorm中的关联操作详解
- 欧惠和后部分气垫对比(夏天油皮仅供参
- 怎样使用对讲机(一张图看懂对讲机)