一、添加注解
在实体类属性上加如下注解
// 后台返回数据到前端的数据格式
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
// 前台传递参数到后台的数据格式
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
二、添加yml配置
spring:
jackson:
## 格式为yyyy-MM-dd HH:mm:ss
date-format: yyyy-MM-dd HH:mm:ss
## 定时区,解决8小时的时间差问题
time-zone: GMT+8
三、全局配置通过Spring
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.text.SimpleDateFormat;
import java.util.List;
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
SimpleModule simpleModule = new SimpleModule();
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(0, jackson2HttpMessageConverter);
}
}
标签:Java,MM,dd,接口,yyyy,mm,HH,import,接收
From: https://www.cnblogs.com/lambertlt/p/17202602.html