SpringBoot如何格式化LocalTime和LocalDate以及LocalDateTime

发布时间:2021-09-28 10:04:35 作者:柒染
来源:亿速云 阅读:235

这篇文章给大家介绍SpringBoot如何格式化LocalTime和LocalDate以及LocalDateTime,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

开篇

好久好久没更新这个文集了,上一次更新我都忘记是什么时间了,原计划Spring Boot系列会写十几篇文章的,现在才写到第7篇(含本文),后续还是会继续更新吧,我一直觉得,写博客的主要目的是梳理自己的技术栈,分享只是附属价值,所以还是要坚持下去的

说说如何格式化Restful接口的时间类型,从JDK8开始提供了更方便日期时间的API,如:LocalTime、LocalDate和LocalDateTime,从此可以远离原来的Date和Calendar类,日期操作变得简单多了。

SpringBoot日期格式化问题

然而,在Spring Boot进行Restful接口开发中使用这些日期时间类型时,你会发现使用jackson的spring.jackson.date-format配置进行日期类型格式化是无效的,为什么呢?

//以下是配置了`spring.jackson.date-format=yyyy-MM-dd HH:mm:ss`后,
//接口返回LocalDateTime和Date的内容,你会发现Date类型的已经格式化,
//但是LocalDateTime却没有。
{
    "localDateTime": "2019-07-14T12:20:23.615",
    "date": "2019-07-14 04:20:23"
}

实际上在Jackson序列化的时候,会根据不同类型调用不同的Serializer进行序列化,如果没有默认的Serializer时,调用的是类的toString()。而Date类型序列化时会使用DateSerializer,里面会使用spring.jackson.date-format中的配置,而LocalDateTime则是通过LocalDateTimeSerializerLocalDateTimeSerializer默认使用的是DateTimeFormatter.ISO_LOCAL_DATE_TIME,所以就会返回上面的结果。

如何处理?

通过上文可以知道,如果能够覆盖默认的LocalDateTimeSerializer ,就可以按照自己的需求进行日期格式化了。

通过以下方式可以实现:

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author Cent.Chen  2019-07-14
 * @Description Dateformat Configuration.
 */
@Configuration
public class DateformatConfig {

    /**
     * Date格式化字符串
     */
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    /**
     * DateTime格式化字符串
     */
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * Time格式化字符串
     */
    private static final String TIME_FORMAT = "HH:mm:ss";

    /**
     * 自定义Bean
     *
     * @return
     */
    @Bean
    @Primary
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
                .serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
                .serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)))
                .deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATETIME_FORMAT)))
                .deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)))
                .deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
    }

}
{
    "localDateTime": "2019-07-14 12:33:11",
    "date": "2019-07-14 04:33:11"
}

关于SpringBoot如何格式化LocalTime和LocalDate以及LocalDateTime就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Spring中使用LocalDateTime、LocalDate等参数作为入参
  2. 怎么在Spring Boot中使用LocalDateTime进行格式化处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:如何理解GridView

下一篇:如何批处理完美实现FTP远程备份数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》