Set Jackson Timezone for Date deserialization

后端 未结 9 611
终归单人心
终归单人心 2020-12-13 04:23

I\'m using Jackson (via Spring MVC Annotations) to deserialize a field into a java.util.Date from JSON. The POST looks like - {\"enrollDate\":\"2011-09-28

9条回答
  •  死守一世寂寞
    2020-12-13 04:45

    If you really want Jackson to return a date with another time zone than UTC (and I myself have several good arguments for that, especially when some clients just don't get the timezone part) then I usually do:

    ObjectMapper mapper = new ObjectMapper();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    dateFormat.setTimeZone(TimeZone.getTimeZone("CET"));
    mapper.getSerializationConfig().setDateFormat(dateFormat);
    // ... etc
    

    It has no adverse effects on those that understand the timezone-p

提交回复
热议问题