Springboot?格式化LocalDateTime的方法
Springboot 格式化LocalDateTime
我們知道在springboot中有默認的json解析器,Spring Boot 中默認使用的 Json 解析技術框架是 jackson。我們點開 pom.xml 中的 spring-boot-starter-web 依賴,可以看到一個 spring-boot-starter-json依賴:
引入依賴
其實引不引入這個依賴都一樣 spring-boot-starter-web 里面就包含這個依賴
就是為了讓你們理解是這個依賴在發(fā)揮作用
<!--而該模塊JSR310支持到了時間類型的序列化、反序列化--> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
配置全局生效
Configuration 標記這是配置類 @Bean注入到spring容器中 @value 獲取參數(shù)
這里配置的格式化日期格式是全局生效 yyyy-MM-dd HH:mm:ss
這里給依賴全路徑 方便導包
這里配置的格式化日期格式是全局生效 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; /** * 文章內(nèi)容 */ @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; /** * 狀態(tài)(0已發(fā)布,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; }
接口測試結(jié)果
1 在沒有加全局日期格式化配置文件的時候
2 加了全局配置類的時候
yyyy-MM-dd HH:mm:ss
3 指定某個字段解析規(guī)則
yyyy-MM-dd
@TableField(value = "create_time") @JsonFormat(pattern = "yyyy-MM-dd") private LocalDateTime createTime;
常用場景
我們一般會配置全局解析的規(guī)則 這樣方便后續(xù)對于時間格式的處理 默認的格式 按照國人的喜好 不太方便 對于后面日期格式個性的要求 我們可以針對某個屬性去設置解析規(guī)則
到此這篇關于Springboot 格式化LocalDateTime的文章就介紹到這了,更多相關Springboot 格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中的instanceof關鍵字在Android中的用法實例詳解
instanceof是Java的一個二元操作符,和==,>,<是同一類東西。接下來通過本文給大家介紹Java中的instanceof關鍵字在Android中的用法,非常不錯,具有參考借鑒價值,感興趣的朋友一起學習吧2016-07-07java實現(xiàn)TCP socket和UDP socket的實例
這篇文章主要介紹了本文主要介紹了java實現(xiàn)TCP socket和UDP socket的實例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02