SpringBoot中关于时间显示格式的处理

Java知识点总结:想看的可以从这里进入

2.21、时间问题

我们从数据库获取的时间,在显示中可能是这样的

image-20230321120136207

所以我们需要在实体类中使用注解@DateTimeFormat(用于前台传递到后台)@JsonFormat(后台传递到前台),来修改时间的格式(推荐使用 JDK8 中增加的LocalDateTime接受数据库DateTime类型的数据。)

/**
* 创建时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime gmtCreate;

/**
* 修改时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime gmtModified;

image-20230321120305843

当然不想每个实体类都添加注解的话,也可以配置全局生效。(@JsonFormat的优先级更高,配置了@JsonFormat后,全局的不生效)

  • 添加注解

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    
  • 编写配置类

    @Configuration
    public class UnifiedTimeProcessing {
          
          
        /** LocalDateTime的格式 */
        public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
        /** LocalDate的格式 */
        public static final String DATE_FORMAT = "yyyy-MM-dd";
        /** LocalTime的格式 */
        public static final String TIME_FORMAT = "HH:mm:ss";
    
        /**
         * LocalDate转换器
         */
        @Bean
        public Converter<String, LocalDate> localDateConverter() {
          
          
            return new Converter<>() {
          
          
                @Override
                public LocalDate convert(String source) {
          
          
                    if (StringUtils.isEmpty(source)) {
          
          
                        return null;
                    }
                    return LocalDate.parse(source, DateTimeFormatter.ofPattern(DATE_FORMAT));
    
                }
            };
        }
    
        /**
         * LocalDateTime转换器
         */
        @Bean
        public Converter<String, LocalDateTime> localDateTimeConverter() {
          
          
            return new Converter<>() {
          
          
                @Override
                public LocalDateTime convert(String source) {
          
          
                    if (StringUtils.isEmpty(source)) {
          
          
                        return null;
                    }
                    return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
                }
            };
        }
    
        /**
         * LocalTime转换器
         */
        @Bean
        public Converter<String, LocalTime> localTimeConverter() {
          
          
            return new Converter<>() {
          
          
                @Override
                public LocalTime convert(String source) {
          
          
                    if (StringUtils.isEmpty(source)) {
          
          
                        return null;
                    }
                    return LocalTime.parse(source, DateTimeFormatter.ofPattern(TIME_FORMAT));
                }
            };
        }
    
        /**
         * 后台到前端 Json序列化转换器
         */
        @Bean
        public ObjectMapper objectMapper() {
          
          
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
    
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            javaTimeModule.addSerializer(LocalDateTime.class,
                    new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
            javaTimeModule.addSerializer(LocalDate.class,
                    new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
            javaTimeModule.addSerializer(LocalTime.class,
                    new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
    
            objectMapper.registerModule(javaTimeModule);
            return objectMapper;
        }
    }
    

猜你喜欢

转载自blog.csdn.net/yuandfeng/article/details/129727974