溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換

發(fā)布時間:2021-12-16 17:16:11 來源:億速云 閱讀:698 作者:小新 欄目:大數(shù)據(jù)

小編給大家分享一下SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

一、LocalDateTime日期轉(zhuǎn)換兩個問題

1.前端傳入?yún)?shù)時String轉(zhuǎn)換為Date

SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot 
deserialize value of type `java.time.LocalDateTime` from String "2020-02-11 12:48:13": Failed to 
deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-11 
12:48:13' could not be parsed at index 10; nested exception is 
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type 
`java.time.LocalDateTime` from String "2020-02-11 12:48:13": Failed to deserialize 
java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-11 12:48:13' could 
not be parsed at index 10
 at [Source: (PushbackInputStream); line: 4, column: 19] (through reference chain: 
com.example.demo.model.Test["localDateTime"])

2.后臺返回值時Date轉(zhuǎn)換為String

LocalDateTime中間有個“T”,其他時間類型時間格式可讀性也很差。

SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換

二、解決方案

編寫一個時間配置類,使用HTTP消息轉(zhuǎn)換器來格式化。

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
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;

/**
 * 時間統(tǒng)一配置
 */
@Configuration
public class DateConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        JavaTimeModule module = new JavaTimeModule();
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return builder -> {
            builder.simpleDateFormat("yyyy-MM-dd hh:mm:ss");
            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
            builder.modules(module);
        };
    }

}
三、驗(yàn)證效果

1.第一個問題解決效果圖:

SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換

2.第二個問題解決效果圖:

SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換

我們可以看到使用了HTTP消息轉(zhuǎn)換器來格式化后,連Date類型也格式化正常了。

看完了這篇文章,相信你對“SpringBoot如何實(shí)現(xiàn)LocalDateTime日期轉(zhuǎn)換”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI