简体   繁体   English

如何使用Jackson反序列化JS日期?

[英]How to deserialize JS date using Jackson?

I'm getting a date string from ExtJS in the format: 我从ExtJS获得一个日期字符串格式:

"2011-04-08T09:00:00" “2011-04-08T09:00:00”

when i try to deserialize this date, it changes the timezone to Indian Standard Time (adds +5:30 to the time) . 当我尝试反序列化此日期时,它会将时区更改为印度标准时间(将时间+5:30添加)。 This is how i'm deserializing the date: 这就是我如何反序化日期:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
getObjectMapper().getDeserializationConfig().setDateFormat(dateFormat);

Doing this also doesn't change the timezone. 这样做也不会改变时区。 I still get the date in IST: 我仍然在IST得到日期:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
getObjectMapper().getDeserializationConfig().setDateFormat(dateFormat);

How do I deserialize the date in the way in which it is coming without the hassles of Timezone? 如何在没有时区麻烦的情况下对日期的日期进行反序列化?

I found a work around but with this I'll need to annotate each date's setter throughout the project. 我找到了一个解决方法但是我需要在整个项目中注释每个日期的setter。 Is there a way in which I can specify the format while creating the ObjectMapper? 有没有一种方法可以在创建ObjectMapper时指定格式?

Here's what I did: 这是我做的:

public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
{
    @Override
    public Date deserialize(JsonParser jsonParser,
            DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String date = jsonParser.getText();
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

}

And annotated each Date field's setter method with this: 并使用以下方法注释每个Date字段的setter方法:

@JsonDeserialize(using = CustomJsonDateDeserializer.class)

This works for me - i am using jackson 2.0.4 这对我有用 - 我使用的是杰克逊2.0.4

ObjectMapper objectMapper = new ObjectMapper();
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
objectMapper.setDateFormat(df);

There is a good blog about this topic: http://www.baeldung.com/jackson-serialize-dates Use @JsonFormat looks the most simple way. 有一个关于这个主题的好博客: http//www.baeldung.com/jackson-serialize-dates使用@JsonFormat看起来是最简单的方法。

public class Event {
    public String name;

    @JsonFormat
      (shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    public Date eventDate;
}

In addition to Varun Achar's answer , this is the Java 8 variant I came up with, that uses java.time.LocalDate and ZonedDateTime instead of the old java.util.Date classes. 除了Varun Achar的回答之外 ,这是我提出的Java 8变体,它使用java.time.LocalDate和ZonedDateTime而不是旧的java.util.Date类。

public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {

    @Override
    public LocalDate deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException {

        String string = jsonparser.getText();

        if(string.length() > 20) {
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(string);
            return zonedDateTime.toLocalDate();
        }

        return LocalDate.parse(string);
    }
  }

@JsonFormat only work for standard format supported by the jackson version that you are using. @JsonFormat仅适用于您使用的jackson版本支持的标准格式。

Ex :- compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) for jackson 2.8.6 例如: - 与任何标准形式兼容(“yyyy-MM-dd'T'HH:mm:ss.SSSZ”,“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”,“ EEE,dd MMM yyyy HH:mm:ss zzz“,”yyyy-MM-dd“))for jackson 2.8.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM