在SpringBoot项目中使用LocalDateTime作为参数与返回并兼容Date

  • 在 SpringWeb 中用 LocalDateTIme 作为接口的返回与参数时会有以下两个问题
  1. 接口返回日期格式中有一个 T
  2. 接口作为参数传入格式化会报错
  • 测试代码
/**
 * 测试 LocalDateTime
 */
@RestController
@RequestMapping("date")
public class LocalDateTimeController {

    @Data
    @Builder
    @AllArgsConstructor
    public static class LocalDateTimeForm {
        private LocalDate date;
        private LocalDateTime dateTime;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LocalDateTimeVO {

        private LocalDateTime localDateTime;

        private LocalDateTimeForm form;

        @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
        private LocalDateTime jsonFormat;

        private Date date;

    }

    @GetMapping
    public LocalDateTimeVO parse(LocalDateTimeForm form) {
        LocalDateTime now = LocalDateTime.now();
        return LocalDateTimeVO.builder().localDateTime(now).jsonFormat(now).date(new Date()).form(form).build();
    }

}
  • 希望达到的效果

在这里插入图片描述

  • 添加配置类
package com.framework.projectframework.configs;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * LocalDateTime 格式化
 *
 * 添加配置
 * spring.jackson.locale=en_CH
 * spring.jackson.time-zone=Asia/Shanghai
 * spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
 *
 * @see <a href="https://www.baeldung.com/spring-boot-formatting-json-dates">https://www.baeldung.com/spring-boot-formatting-json-dates</a>
 * @see <a href="https://janus.blog.csdn.net/article/details/107065047">https://janus.blog.csdn.net/article/details/107065047</a>
 * @see <a href="https://segmentfault.com/a/1190000022512201">https://segmentfault.com/a/1190000022512201</a>
 */
@Configuration
public class ChineseLocalDateTimeFormat {

    @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String dateTimeFormat;

    private static final String dateFormat = "yyyy-MM-dd";

    /**
     * 作为返回的 格式化
     * @return
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            builder.simpleDateFormat(dateTimeFormat);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
        };
    }

    /**
     * 作为参数的 日期格式化
     */
    @Bean
    public Formatter<LocalDate> localDateFormatter() {
        return new Formatter<LocalDate>() {

            private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat);

            @Override
            public LocalDate parse(String text, Locale locale) throws ParseException {
                return LocalDate.parse(text, dateTimeFormatter);
            }

            @Override
            public String print(LocalDate localDate, Locale locale) {
                return dateTimeFormatter.format(localDate);
            }

        };
    }

    /**
     * 作为参数的 日期时间格式化
     */
    @Bean
    public Formatter<LocalDateTime> localDateTimeFormatter() {
        return new Formatter<LocalDateTime>() {

            private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);

            @Override
            public LocalDateTime parse(String text, Locale locale) throws ParseException {
                return LocalDateTime.parse(text, formatter);
            }

            @Override
            public String print(LocalDateTime localDateTime, Locale locale) {
                return formatter.format(localDateTime);
            }

        };
    }

}
  • 配置文件中配置date的格式化
spring:
  jackson:
    locale: en_CH
    time-zone: Asia/Shanghai
    date-format: yyyy-MM-dd HH:mm:ss
  • 这些都是我测试好实践的, 如果想详细研究一下可以看一下

  • https://janus.blog.csdn.net/article/details/107065047
  • https://www.baeldung.com/spring-boot-formatting-json-dates
  • https://segmentfault.com/a/1190000022512201
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值