SpringBoot(十一):LocalDateTime格式化日期

版权声明

本作品采用知识共享署名 4.0 国际许可协议进行许可。
本文作者:低调小熊猫
文章链接:https://aodeng.cc/archives/springbootshiyi
转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032

单纯的广告

个人博客:https://aodeng.cc
微信公众号:低调小熊猫
qq交流群:756796932

简介

很多时候日期格式输出是这样的
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"}

源码地址

https://github.com/java-aodeng/hope

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值