04 Springboot 格式化LocalDateTime
Springboot 格式化LocalDateTime
我们知道在springboot中有默认的json解析器,Spring Boot 中默认使用的 Json 解析技术框架是 jackson。我们点开 pom.xml 中的 spring-boot-starter-web 依赖,可以看到一个 spring-boot-starter-json依赖:
引入依赖
其实引不引入这个依赖都一样 spring-boot-starter-web 里面就包含这个依赖
就是为了让你们理解是这个依赖在发挥作用
<!--而该模块JSR310支持到了时间类型的序列化、反序列化-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
配置全局生效
Configuration 标记这是配置类 @Bean注入到spring容器中 @value 获取参数
这里配置的格式化日期格式是全局生效 yyyy-MM-dd HH:mm:ss
这里给依赖全路径 方便导包
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
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 LocalDateTimeSerializerConfig {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String pattern;
public LocalDateTimeSerializer localDateTimeDeserializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
}
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
// 默认LocalDateTime格式化的格式 yyyy-MM-dd HH:mm:ss
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
}
}
**实体类 **
日期类型是 LocalDateTime
@Data
@EqualsAndHashCode(callSuper = false)
@TableName(value = "sg_article")
public class Article implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 标题
*/
@TableField(value = "title")
private String title;
/**
* 文章内容
*/
@TableField(value = "content")
private String content;
/**
* 文章摘要
*/
@TableField(value = "summary")
private String summary;
/**
* 所属分类id
*/
@TableField(value = "category_id")
private Long categoryId;
/**
* 所属分类名称
*/
@TableField(exist = false)
private String categoryName;
/**
* 缩略图
*/
@TableField(value = "thumbnail")
private String thumbnail;
/**
* 是否置顶(0否,1是)
*/
@TableField(value = "is_top")
private String isTop;
/**
* 状态(0已发布,1草稿)
*/
@TableField(value = "status")
private String status;
/**
* 访问量
*/
@TableField(value = "view_count")
private Long viewCount;
/**
* 是否允许评论 1是,0否
*/
@TableField(value = "is_comment")
private String isComment;
@TableField(value = "create_by")
private Long createBy;
@TableField(value = "create_time")
private LocalDateTime createTime;
@TableField(value = "update_by")
private Long updateBy;
@TableField(value = "update_time")
private LocalDateTime updateTime;
/**
* 删除标志(0代表未删除,1代表已删除)
*/
@TableField(value = "del_flag")
private Integer delFlag;
}
接口测试结果
1 在没有加全局日期格式化配置文件的时候

2 加了全局配置类的时候
yyyy-MM-dd HH:mm:ss

3 指定某个字段解析规则
yyyy-MM-dd
@TableField(value = "create_time")
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDateTime createTime;

常用场景
- 我们一般会配置全局解析的规则 这样方便后续对于时间格式的处理 默认的格式 按照国人的喜好 不太方便 对于后面日期格式个性的要求 我们可以针对某个属性去设置解析规则
04 Springboot 格式化LocalDateTime的更多相关文章
- springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)--thymeleaf格式化LocalDateTime,LocalDate等JDK8新时间类
依赖maven包 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>th ...
- springboot格式化时间
使用@RestController注解,返回的java对象中若含有date类型的属性,则默认输出为TIMESTAMP时间戳格式,可以在配置文件加入下面配置 spring.jackson.date-fo ...
- (04) springboot 下的springMVC和jsp和mybatis
1. springboot 和springmvc下的注解完全一样(新增了一些有用的) 常用的注解如下: @Controller @RestController= @Controller + @Resp ...
- springboot 格式化返回日期
两种方式: 1,可以在 pojo 类每个属性添加 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8&qu ...
- 【SpringBoot】04.SpringBoot整合Filter的两种方式
SpringBoot整合Filter过滤器的两种方式: 1.通过扫描注解完成Filter组件注册 创建一个类,实现Filter接口,实现doFilter()方法 在该类使用注解@WebFilter,设 ...
- SpringBoot中LocalDatetime作为参数和返回值的序列化问题
欢迎访问我的个人网站 https://www.zhoutao123.com 本文原文地址 https://www.zhoutao123.com/#/blog/article/59 LocalDatet ...
- 【转载】java8中的Calendar日期对象(LocalDateTime)
Java 8 推出了全新的日期时间API,Java 8 下的 java.time包下的所有类都是不可变类型而且线程安全. 下面是新版API中java.time包里的一些关键类: Instant:瞬时实 ...
- java8 Date Localdatetime instant 相互转化(转) 及当天的最大/最小时间
Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant instant)和toInstant()方法 // Obtains an instance of Dat ...
- SpringBoot简单尝试
一.spring boot核心 配置在类路径下autoconfigure下(多瞅瞅) @SpringBootApplication里的重要注解(@Configuration,@EnableAutoCo ...
随机推荐
- 顺利通过EMC实验(17)
- 在VisualStudio调试器中使用内存窗口和查看内存分布
调试模式下内存窗口的使用 在调试期间,"内存"窗口显示应用使用的内存空间.调试器窗口(如"监视"."自动"."局部变量" ...
- 【SpringBoot实战】核心配置和注解
前言 SpringBoot核心配置在springboot中有非常重要的作用,我们可是使用核心配置文件进行一些基础功能的定义,属性值的注入等.springboot支持两种格式的核心配置文件,一种是pro ...
- 小程序完整对接 pingpp支付
小程序完整对接 pingpp支付 有几个先要条件: 小程序需要企业认证且开通支付功能,个人认证是无法使用支付功能的(小程序微信支付官网) pingpp 本身接入的企业服务器(即商户服务器)并不强制要求 ...
- ES6-11学习笔记--数组遍历
ES5中数组遍历方式: for循环 forEach():没有返回值,只是针对每个元素调用func map():返回新的Array,每个元素为调用func的结果 filter():返回符合func条件的 ...
- 人机交互大作业--flash嵌入web(纯界面)
界面: 源代码:最近较忙,后续会上传至github
- This program may be freely redistributed under the terms of the GNU GPL
在centos中安装Google浏览器时 执行[root@server1 opt]# rpm ivh install google-chrome-stable_current_x86_64.rpm 爆 ...
- 小程序申请测试appid
话不多说,直接上图: 1. 登录微信官方文档: https://developers.weixin.qq.com/miniprogram/dev/devtools/sandbox.html 2. ...
- 小程序安卓端播放不了音频解决方法wx.createInnerAudioContext()
在小程序播放音频时,使用组件wx.createInnerAudioContext(),安卓端无法播放音频. 我的情况:播放服务器上传来的音频,格式为mp3.首先查看你的格式是否符合文档要求 在安卓端进 ...
- 两数之和_LeetCode_1
LeetCode_1原题链接:https://leetcode-cn.com/problems/two-sum/ 剑指 Offer 57原题链接: https://leetcode-cn.com/pr ...
