【关于Jackson库中objectMapper用法中对象中date类型转JSONString后日期减一天问题】

关于Jackson库中objectMapper用法中对象中date类型转JSONString后日期减一天问题

问题描述:

示例:

/**
     * 操作时间
     */
    @JsonFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty(value = "操作时间", name = "operationTime")
    private Date operationTime;
String jsonStr = mapper.writeValueAsString(Object obj);

当对象中有日期类型,使用JsonFormat"yyyy-MM-dd" 后发现日期转换后会减一天;

原因:未指定时间导致的转换的时候默认将日期按照国际化时区进行转换导致减8小时后减少了一天

解决方法:
添加 timezone=“GMT+8” 时区解决

 /**
     * 操作时间
     */
    @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
    @ApiModelProperty(value = "操作时间", name = "operationTime")
    private Date operationTime;

你可能感兴趣的:(java,开发语言)