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 ...
随机推荐
- html5手机页面的那些meta
一.普通手机页的设置1.<meta name="viewport" content=""/>说明:屏幕的缩放 content的几个属性: width ...
- the compatibility problem of ie
ie8hack ie8下的兼容问题处理:背景透明,css3圆角,css3和jquery支持部分css3选择器(例如:nth-child),支持html5的语义化标签,媒体查询@media等. 在htm ...
- 让我为你介绍一个神器:Chimee,一套可扩展的 H5 视频播放器解决方案
Chimee(读"奇米", [tʃɪ'mɪ:])是由奇舞团开源的一套 H5 视频播放器解决方案,由奇舞团视频云前端团队结合在业务和视频编解码方向的沉淀积累倾心打造. Chimee ...
- 【weex开发】weex官方源码
公司目前使用版本:weex_sdk:0.10.0 介绍地址:https://bintray.com/alibabaweex/maven/weex_sdk/0.18.0 weex最新版本:weex_sd ...
- oracle 多列求和
第一种: select sum(decode(count1,null,0,count1) +decode(count2,null,0,count2) +decode(count3,null,0,cou ...
- Input的校验表达式
1.只是不能输入空格 <input type="text" onkeyup="this.value=this.value.replace(/^ +| +$/g,'' ...
- Blazor 生命周期
执行周期 1. SetParametersAsync 2. OnInitializedAsync(调用两次) 和 OnInitialized: 3. OnParametersSetAsync 或 On ...
- Vue实战-购物车案例
Vue实战-购物车案例 普通购物车 实现的功能:添加商品到购物车,计算总价 <!DOCTYPE html> <html lang="en"> <hea ...
- android软件简约记账app开发day05-记账页面条目代码优化和bug解决
android软件简约记账app开发day05-记账页面条目代码优化和bug解决 今天还是因为该bug又极大的耽误了项目进程,该开发文档都要没有时间来写了. 先说bug吧,在昨天已经实现了页面图标的展 ...
- linux脚本执行jar包运行
以下为linux下运行jar包的脚本(只需替换jar包名称): #!/bin/bash #这里可替换为你自己的执行程序,其他代码无需更改 APP_NAME=ruoyi-admin.jar cd `di ...