Java 8之后,日期类的处理建议使用java.time包中对应的LocalDateTime, LocalDate, LocalTime类。

1、注解

@JsonFormat主要是后台到前台的时间格式的转换,即从数据库读取数据到后端的过程,序列化
@DateTimeFormat主要是前端到后端的时间格式的转换,反序列化
示例:

  1. //数据库到后端的转换
  2. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  3. //后端到前端的转换
  4. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

springboot中默认使用jackson做json序列化和反序列化

2 、全局配置序列化

  1. /**
  2. * LocalDateTime序列化配置
  3. */
  4. @Configuration
  5. public class LocalDateTimeSerializerConfig {
  6. /*
  7. 如果在配置文件中有spring.jackson.date-format,则取其中的值,
  8. 如果没有配置,则为yyyy-MM-dd HH:mm:ss
  9. */
  10. @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
  11. private String pattern;
  12. // localDateTime 序列化器
  13. @Bean
  14. public LocalDateTimeSerializer localDateTimeSerializer() {
  15. return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
  16. }
  17. @Bean
  18. public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
  19. return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
  20. }
  21. }

3、日期转换

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.core.convert.converter.Converter;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.LocalTime;
  7. import java.time.format.DateTimeFormatter;
  8. @Configuration
  9. public class DateConverterConfig {
  10. @Bean
  11. public Converter<String, LocalDateTime> LocalDateTimeConvert() {
  12. return new Converter<String, LocalDateTime>() {
  13. @Override
  14. public LocalDateTime convert(String source) {
  15. DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  16. LocalDateTime date = null;
  17. try {
  18. date = LocalDateTime.parse(source, df);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return date;
  23. }
  24. };
  25. }
  26. @Bean
  27. public Converter<String, LocalDate> LocalDateConvert() {
  28. return new Converter<String, LocalDate>() {
  29. @Override
  30. public LocalDate convert(String source) {
  31. DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  32. LocalDate date = null;
  33. try {
  34. date = LocalDate.parse(source, df);
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. return date;
  39. }
  40. };
  41. }
  42. @Bean
  43. public Converter<String, LocalTime> LocalTimeConvert() {
  44. return new Converter<String, LocalTime>() {
  45. @Override
  46. public LocalTime convert(String source) {
  47. DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm:ss");
  48. LocalTime date = null;
  49. try {
  50. date = LocalTime.parse(source, df);
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. return date;
  55. }
  56. };
  57. }
  58. }

4、 注意事项

实体类必须实现序列化。

对application.yml 配置日期

  1. spring:
  2. mvc:
  3. date-format: yyyy-MM-dd HH:mm:ss
  4. jackson:
  5. date-format: yyyy-MM-dd HH:mm:ss

5、去除时间中间的T

  1. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
  2. String strLocalDate = "2015-10-23T03:34:40";
  3. LocalDateTime localDate = LocalDateTime.parse(strLocalDate, formatter);
  4. System.out.println(localDate);
  5. System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDate));
  6. System.out.println(DateTimeFormatter.ofPattern("HH:mm:ss yyyy-MM-dd ").format(localDate));

打印

  1. 2015-10-23T03:34:40
  2. 2015-10-23 03:34:40
  3. 03:34:40 2015-10-23
  1. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(deliverGoods.getCreateTime())