Set Jackson Timezone for Date deserialization

后端 未结 9 576
终归单人心
终归单人心 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 05:00

    I had same problem with Calendar deserialization, solved extending CalendarDeserializer.
    It forces UTC Timezone
    I paste the code if someone need it:

    @JacksonStdImpl
    public class UtcCalendarDeserializer extends CalendarDeserializer {
    
        TimeZone TZ_UTC = TimeZone.getTimeZone("UTC");
    
        @Override
        public Calendar deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            JsonToken t = jp.getCurrentToken();
            if (t == JsonToken.VALUE_NUMBER_INT) {
                Calendar cal = Calendar.getInstance(TZ_UTC);
                cal.clear();
                cal.setTimeInMillis(jp.getLongValue());
    
                return cal;
            }
    
            return super.deserialize(jp, ctxt);
        }
    }
    

    in JSON model class just annotate the field with:

    @JsonDeserialize(using = UtcCalendarDeserializer.class)
    private Calendar myCalendar;
    
    0 讨论(0)
  • 2020-12-13 05:01

    Looks like older answers were fine for older Jackson versions, but since objectMapper has method setTimeZone(tz), setting time zone on a dateFormat is totally ignored.

    How to properly setup timeZone to the ObjectMapper in Jackson version 2.11.0:

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setTimeZone(TimeZone.getTimeZone("Europe/Warsaw"));
    

    Full example

      @Test
      void test() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        JavaTimeModule module = new JavaTimeModule();
        objectMapper.registerModule(module);
        objectMapper.setTimeZone(TimeZone.getTimeZone("Europe/Warsaw"));
    
        ZonedDateTime now = ZonedDateTime.now();
        String converted = objectMapper.writeValueAsString(now);
    
        ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
        System.out.println("serialized: " + now);
        System.out.println("converted: " + converted);
        System.out.println("restored: " + restored);
    
        Assertions.assertThat(now).isEqualTo(restored);
      }
    `
    
    0 讨论(0)
  • 2020-12-13 05:07

    Your date object is probably ok, since you sent your date encoded in ISO format with GMT timezone and you are in EST when you print your date.

    Note that Date objects perform timezone translation at the moment they are printed. You can check if your date object is correct with:

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);
    System.out.println (cal);
    
    0 讨论(0)
提交回复
热议问题