1.配置文件修改(范围修改)
spring:jackson:# 东8 北京时区time-zone: GMT+8# 日期格式date-format: yyyy-MM-dd HH:mm:ss
2.Java代码修改(范围修改)
2.1 时区
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.TimeZone;@Configuration
public class TimeConfig {@Beanpublic void setDefaultTimeZone() {TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");TimeZone.setDefault(timeZone);}
}
2.2 日期格式
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;@Configuration
public class JacksonConfig {@Beanpublic MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {ObjectMapper objectMapper = new ObjectMapper();// 设置日期格式为yyyy-MM-dd HH:mm:ssobjectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));return new MappingJackson2HttpMessageConverter(objectMapper);}
}
3. Java代码修改(局部修改)
@ApiModelProperty("创建时间")@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createTime;
局部修改优先级高于范围修改。