Java 8之后,日期类的处理建议使用java.time包中对应的LocalDateTime, LocalDate, LocalTime类。
1、注解
@JsonFormat主要是后台到前台的时间格式的转换,即从数据库读取数据到后端的过程,序列化
@DateTimeFormat主要是前端到后端的时间格式的转换,反序列化
示例:
//数据库到后端的转换
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
//后端到前端的转换
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
springboot中默认使用jackson做json序列化和反序列化
2 、全局配置序列化
/**
* LocalDateTime序列化配置
*/
@Configuration
public class LocalDateTimeSerializerConfig {
/*
如果在配置文件中有spring.jackson.date-format,则取其中的值,
如果没有配置,则为yyyy-MM-dd HH:mm:ss
*/
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
// localDateTime 序列化器
@Bean
public LocalDateTimeSerializer localDateTimeSerializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
}
}
3、日期转换
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class DateConverterConfig {
@Bean
public Converter<String, LocalDateTime> LocalDateTimeConvert() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = null;
try {
date = LocalDateTime.parse(source, df);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
};
}
@Bean
public Converter<String, LocalDate> LocalDateConvert() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = null;
try {
date = LocalDate.parse(source, df);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
};
}
@Bean
public Converter<String, LocalTime> LocalTimeConvert() {
return new Converter<String, LocalTime>() {
@Override
public LocalTime convert(String source) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime date = null;
try {
date = LocalTime.parse(source, df);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
};
}
}
4、 注意事项
实体类必须实现序列化。
对application.yml 配置日期
spring:
mvc:
date-format: yyyy-MM-dd HH:mm:ss
jackson:
date-format: yyyy-MM-dd HH:mm:ss
5、去除时间中间的T
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
String strLocalDate = "2015-10-23T03:34:40";
LocalDateTime localDate = LocalDateTime.parse(strLocalDate, formatter);
System.out.println(localDate);
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDate));
System.out.println(DateTimeFormatter.ofPattern("HH:mm:ss yyyy-MM-dd ").format(localDate));
打印
2015-10-23T03:34:40
2015-10-23 03:34:40
03:34:40 2015-10-23
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(deliverGoods.getCreateTime())