专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

springboot中LocalDateTime格式化日期

很多时候日期格式输出是这样的
2018-10-09T17:39:07.097
中间有个T,尴尬,是的我们需要去掉这个T
这方法是springboot封装好了的,我们直接使用即可,普通的配置我就不贴了

教程

一、全局配置模式

创建日期config类


@Configuration public class LocalDateTimeSerializerConfig { @org.springframework.beans.factory.annotation.Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }

实体类,get,set忽略


public class TestEntity { private String name; private LocalDateTime dateTimes; }

controller使用


@GetMapping("/test") public TestEntity test(){ TestEntity testEntity=new TestEntity(); testEntity.setName("admin"); testEntity.setDateTimes(LocalDateTime.now()); return testEntity; }

成功效果


{"name":"admin","dateTimes":"2018-10-09 17:39:07"}

二,最为现在常用的方式

@JsonFormat注解


public class OrderInfo { @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime; @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; public LocalDateTime getCreateTime() { return createTime; } public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }

文章永久链接:https://tech.souyunku.com/24769

未经允许不得转载:搜云库技术团队 » springboot中LocalDateTime格式化日期

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们