千家信息网

SpringBoot如何实现LocalDateTime日期转换

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,小编给大家分享一下SpringBoot如何实现LocalDateTime日期转换,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、LocalDateTime日期转换两个问题1.前端传
千家信息网最后更新 2026年11月02日SpringBoot如何实现LocalDateTime日期转换

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

一、LocalDateTime日期转换两个问题

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

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",其他时间类型时间格式可读性也很差。

二、解决方案

编写一个时间配置类,使用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;/** * 时间统一配置 */@Configurationpublic 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.第一个问题解决效果图:

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

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

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

时间 格式 日期 效果 问题 效果图 消息 篇文章 类型 转换器 配置 两个 前端 参数 可读性 后台 完了 方案 更多 知识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 什么是科技互联网 哈尔滨大商圈互联网科技有限公司 中国互联网科技工作委员会毛军 上海挚品互联网科技 美国互联网黑科技 互联网科技上市公司 富国互联网科技创新基金 互联网实体化科技化 中国科技互联网公司收入 南山科技园互联网公司有哪些 成都铂石互联网科技做什么的 青岛科技大学互联网大赛研究生 苏州铭阳互联网科技有限公司 亳州好赞互联网科技有限公司 玉溪新华互联网科技好不好 工业互联网展湾科技股份有限公司 广州三三互联网科技有限公司 黑龙江互联网科技公司 互联网科技创业计划的风险分析 广东猫王互联网科技有限公司电话 互联网公司会议桌创意科技软装饰 青海省互联网新科技 卫星互联网科技股票 互联网科技寓意 宁夏科技产业互联网案例 互联网为啥属于高科技 本届互联网大会上的黑科技 国产互联网科技公司 互联网领先科技活动 长沙易易互联网科技有限公司
0