方式一:全局设置
在JacksonUtil工具方法初始化实例的时候,指定日期格式
static{
objectMapper =new ObjectMapper();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.setDateFormat(format);
}
方式二:按需设置
增加Jackson日期工具类
public class JsonDateFormatFull extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(value);
gen.writeString(formattedDate);
}
}
在需求日期格式化的字段上加属性
private Long id;
private String name;
@JsonSerialize(using=JsonDateFormatFull.class)
private Date createTime;