SpringBoot如何实现LocalDateTime日期转换

发布时间:2021-12-16 17:16:11 作者:小新
来源:亿速云 阅读:636

小编给大家分享一下SpringBoot如何实现LocalDateTime日期转换,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

一、LocalDateTime日期转换两个问题

1.前端传入参数时String转换为Date

SpringBoot如何实现LocalDateTime日期转换

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot 
deserialize value of type `java.time.LocalDateTime` from String "2020-02-11 12:48:13": Failed to 
deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-11 
12:48:13' could not be parsed at index 10; nested exception is 
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type 
`java.time.LocalDateTime` from String "2020-02-11 12:48:13": Failed to deserialize 
java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-11 12:48:13' could 
not be parsed at index 10
 at [Source: (PushbackInputStream); line: 4, column: 19] (through reference chain: 
com.example.demo.model.Test["localDateTime"])

2.后台返回值时Date转换为String

LocalDateTime中间有个“T”,其他时间类型时间格式可读性也很差。

SpringBoot如何实现LocalDateTime日期转换

二、解决方案

编写一个时间配置类,使用HTTP消息转换器来格式化。

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

/**
 * 时间统一配置
 */
@Configuration
public class DateConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        JavaTimeModule module = new JavaTimeModule();
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return builder -> {
            builder.simpleDateFormat("yyyy-MM-dd hh:mm:ss");
            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
            builder.modules(module);
        };
    }

}
三、验证效果

1.第一个问题解决效果图:

SpringBoot如何实现LocalDateTime日期转换

2.第二个问题解决效果图:

SpringBoot如何实现LocalDateTime日期转换

我们可以看到使用了HTTP消息转换器来格式化后,连Date类型也格式化正常了。

看完了这篇文章,相信你对“SpringBoot如何实现LocalDateTime日期转换”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. springboot日期转换器实现实例解析
  2. 如何解决springboot mybatis里localdatetime序列化问题

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

springboot localdatetime

上一篇:大规模Elasticsearch集群管理心得是什么

下一篇:怎么解析Python中的Dict

相关阅读

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

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