解决objectToMap时区问题

Java ObjectToMap(对象转Map)

Java ObjectMapper.convertValue方法代码可能会出现时区问题,mysql从数据库查询/保存的时间与实际时间相差8小时/1天
 private Map<?, ?> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Map<?, ?> mappedObject = mapper.convertValue(obj, Map.class);
        return mappedObject;
    }

尝试通过配置文件方式配置不生效

#application.yml文件配置
spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss

最终解决

public class Vo {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date startTime;
}

你可能感兴趣的:(java,springboot,java,spring,spring,boot)