修改配置文件配置application.yml:
spring:
# 配置日期格式化
jackson:
date-format: yyyy-MM-dd HH:mm:ss #时间戳统一转换为指定格式
time-zone: GMT+8 # 时区修改为东8区
application.properties配置方式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss #时间戳统一转换为指定格式
spring.jackson.time-zone=GMT+8 # 时区修改为东8区
【注意】这里需要修改时区time-zone:数据库默认时区是格林尼治的时间,如果不设置,会比实际时间少8个小时(北京时间)。
@JsonFormat注解
@JsonFormat
注解用于属性或方法上,将Date类型转换为我们需要的类型显示。
//在pattern上设置自己需要的格式
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date createTime;
@DateTimeFormat注解
在需要进行日期格式转换的Date属性上添加注解@DateTimeFormat(pattern = "需要转换的格式")
//注解将yyyy-MM-dd的形式转换为Date数据
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date birthday;
@Temporal注解
通过@Temporal
注解,实现日期格式转换,它自带属性参数,比如:
@Entity
public class MyEntity {
@Id
private Long id;
// 得到YYYY-MM-DD日期格式
@Temporal(TemporalType.DATE)
private Date myDate;
// 得到HH:MM:SS时间格式
@Temporal(TemporalType.TIME)
private Date myTime;
// 得到YYYY-MM-DD HH:MM:SS时间格式
@Temporal(TemporalType.TIMESTAMP)
private Date myTimestamp;
// other fields and methods
}
package com.mooding.common.share.config;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.jackson.JsonComponent;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.mooding.common.tools.utils.DateUtils;
import lombok.SneakyThrows;
/**
* 类UserCombinedSerializer的实现描述
*
*/
@JsonComponent
@ConditionalOnProperty(prefix = DateFormatConfig.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class DateFormatConfig {
public static final String PREFIX = "mooding.localdateformat";
/**
* 中划线
*/
public static final String STRIKETHROUGH = "-";
public static class LocalDateTimeJsonSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString(DateUtils.FMT.YYYY_MM_DD_HH_MM_SS.format(value));
}
}
public static class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
return DateUtils.FMT.YYYY_MM_DD_HH_MM_SS.parseLocalDateTime(jsonParser.getText());
}
}
public static class DateJsonSerializer extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.FMT.YYYY_MM_DD_HH_MM_SS.getPattern());
jsonGenerator.writeString(sdf.format(value));
}
}
public static class DateJsonDeserializer extends JsonDeserializer<Date> {
@SneakyThrows
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
String text = jsonParser.getText();
if (text.contains(STRIKETHROUGH)) {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.FMT.YYYY_MM_DD_HH_MM_SS.getPattern());
return sdf.parse(jsonParser.getText());
} else {
// 时间戳转换
return new Date(Long.parseLong(text));
}
}
}
public static class LocalDateJsonSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString(DateUtils.FMT.YYYY_MM_DD.format(value));
}
}
public static class LocalDateJsonDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
return DateUtils.FMT.YYYY_MM_DD.parseLocalDate(jsonParser.getText());
}
}
public static class LocalTimeJsonSerializer extends JsonSerializer<LocalTime> {
@Override
public void serialize(LocalTime value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString(DateUtils.FMT.HH_MM_SS.format(value));
}
}
public static class LocalTimeJsonDeserializer extends JsonDeserializer<LocalTime> {
@Override
public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
return DateUtils.FMT.HH_MM_SS.parseLocalTime(jsonParser.getText());
}
}
}
?