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;
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰 羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值