詳解Java關(guān)于時(shí)間格式化的方法
一般從數(shù)據(jù)庫獲取的時(shí)間或日期時(shí)間格式化為date或者datetime,為了方便前端渲染,API接口返回的時(shí)候需要對日期進(jìn)行格式化轉(zhuǎn)換,通常會(huì)用到 SimpleDateFormat
工具處理。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); String time = dateFormat.format(new Date());
如果一個(gè)DTO類里面有很多關(guān)于時(shí)間字段需要格式化,就會(huì)降低開發(fā)效率,產(chǎn)生很多重復(fù)臃腫的代碼。并且有的項(xiàng)目用Date,有的項(xiàng)目會(huì)用LocalDateTime
而此時(shí)如果能將時(shí)間格式統(tǒng)一配置,就可以省下更多時(shí)間專注于業(yè)務(wù)開發(fā)了。
接下來介紹SpringBoot中常用的對時(shí)間或日期處理的方式
一、@JsonFormat 注解
JsonFormat注解是jackson包里面的一個(gè)注解,需要加上依賴
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.2</version> </dependency>
@JsonFormat注解需要用在實(shí)體類的時(shí)間字段上,對應(yīng)的字段才能進(jìn)行格式化。
import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.time.LocalDateTime; import java.util.Date; @Data public class TestDTO { @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd") private LocalDateTime createTime; @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; }
public TestDTO get(){ TestDTO testDTO = new TestDTO(); testDTO.setLocalDateTime(LocalDateTime.now()); testDTO.setDate(new Date()); return testDTO; }
如下所示
還有一種可以全局定義的
二、@JsonComponent 注解 (全局)
配置類
@JsonComponent public class DateFormatConfig { @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; // date 類型全局時(shí)間格式化 @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() { return builder -> { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat(pattern); df.setTimeZone(tz); builder.failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df); }; } // LocalDate 類型全局時(shí)間格式化 @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }
這樣我們就不用加注解了,也可以實(shí)現(xiàn)格式化
@JsonComponent public class DateFormatConfig { @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; // date 類型全局時(shí)間格式化 @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() { return builder -> { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat(pattern); df.setTimeZone(tz); builder.failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df); }; } // LocalDate 類型全局時(shí)間格式化 @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }
到此這篇關(guān)于詳解Java關(guān)于時(shí)間格式化的方法的文章就介紹到這了,更多相關(guān)Java 時(shí)間格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot實(shí)現(xiàn)快遞物流查詢功能(快遞鳥)
本文將基于springboot2.4.0實(shí)現(xiàn)快遞物流查詢,物流信息的獲取通過快遞鳥第三方實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-10-10springcloud gateway自定義斷言規(guī)則詳解,以后綴結(jié)尾進(jìn)行路由
這篇文章主要介紹了springcloud gateway自定義斷言規(guī)則詳解,以后綴結(jié)尾進(jìn)行路由,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot JSON全局日期格式轉(zhuǎn)換器實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot JSON全局日期格式轉(zhuǎn)換器,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Java中List排序的三種實(shí)現(xiàn)方法實(shí)例
其實(shí)Java針對數(shù)組和List的排序都有實(shí)現(xiàn),對數(shù)組而言你可以直接使用Arrays.sort,對于List和Vector而言,你可以使用Collections.sort方法,下面這篇文章主要給大家介紹了關(guān)于Java中List排序的三種實(shí)現(xiàn)方法,需要的朋友可以參考下2021-12-12Java 反射機(jī)制知識(shí)詳細(xì)介紹及總結(jié)
反射機(jī)制是在運(yùn)行狀態(tài)中,對于任意一個(gè)類,都能夠知道這個(gè)類的所有屬性和方法;對于任意一個(gè)對象,都能夠調(diào)用它的任意一個(gè)方法和屬性;這種動(dòng)態(tài)獲取的信息以及動(dòng)態(tài)調(diào)用對象的方法的功能稱為java語言的反射機(jī)制2017-01-01