日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

LocalDateTime去掉T

 Coder編程 2021-05-12

最近在使用阿里巴巴的fastjson反序列化對象的時候,對象里面時間格式屬性總是會多了一個T

 2021-1-09T18:29:09.097

這個T是啥國際標(biāo)準(zhǔn),但是我們的前端又不需要這個T,所以就要想辦法去掉T。

先看代碼

1、添加 LocalDateTimeSerializerConfig ,這個照搬過去就行了

 1 package com.dt.cloud.config;
 2 
 3 import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
 4 import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import java.time.LocalDateTime;
 8 import java.time.format.DateTimeFormatter;
 9 
10 /**
11  * @Description: 去掉LocalDateTime帶T的問題
12  * @author: Zhang Chonghu
13  * @Date: 2021/1/27 9:51
14  * @Copyright: Xi'an Dian Tong Software Co., Ltd. All Rights Reserved.
15  * @Version 1.0
16  */
17 @Configuration
18 public class LocalDateTimeSerializerConfig {
19     @org.springframework.beans.factory.annotation.Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
20     private String pattern;
21     @Bean
22     public LocalDateTimeSerializer localDateTimeDeserializer() {
23         return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
24     }
25     @Bean
26     public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
27         return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
28     }
29 
30 }

 

在給小白們看看這個目錄,我們項目是有個config包專門管理這些config配置類的

 

 

2、做完配置,就要做一些代碼的操作了

 

 1     @JSONField(name="CreateTime")
 2     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
 3     private LocalDateTime createTime;
 4 
 5     @JSONField(name="CreateBy")
 6     private Integer createBy;
 7 
 8     @JSONField(name="ChangeTime")
 9     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
10     private LocalDateTime changeTime;

 

注意,我這兩個時間屬性是在Queue實體類中的,這里就貼這兩個屬性的代碼出來。這里要做的就是加上@JsonFormat,上圖

 

 

3、重點來了,注意看,我是先重數(shù)據(jù)庫中查詢出queue這個對象,在利用阿里巴巴直接序列化這個queue對象

 

 

這里要說明的是,因為業(yè)務(wù)需要,我要把整個對象返回,所以要序列化這個對象,再序列化那兩個時間屬性,如果我們只需要時間屬性,不需要其他屬性,直接序列化時間屬性返回就行了。

0k,下課!?。?/p>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多