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

栏目: Java · 发布时间: 7年前

内容简介:本作品采用本文作者:低调小熊猫文章链接:

本作品采用 知识共享署名 4.0 国际许可协议 进行许可。

本文作者:低调小熊猫

文章链接: https://aodeng.cc/archives/springbootshiyi

转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032

简介

很多时候日期格式输出是这样的

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"}

以上所述就是小编给大家介绍的《SpringBoot(十一):LocalDateTime格式化日期》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

关乎天下

关乎天下

关明生 / 学林出版社 / 2005-8 / 8.00元

《关乎天下:中小企业赢取江山的秘诀》收录了作者多年在大企业(如阿里巴巴网站)从事管理基层得到的经验,可为众多中小企业提供招贤纳士和销售管理方面的新思路。一起来看看 《关乎天下》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具