国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

詳解Java關于時間格式化的方法

瀏覽:6日期:2022-08-25 14:08:10

一般從數據庫獲取的時間或日期時間格式化為date或者datetime,為了方便前端渲染,API接口返回的時候需要對日期進行格式化轉換,通常會用到 SimpleDateFormat 工具處理。

SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd');String time = dateFormat.format(new Date());

如果一個DTO類里面有很多關于時間字段需要格式化,就會降低開發效率,產生很多重復臃腫的代碼。并且有的項目用Date,有的項目會用LocalDateTime

而此時如果能將時間格式統一配置,就可以省下更多時間專注于業務開發了。

接下來介紹SpringBoot中常用的對時間或日期處理的方式

一、@JsonFormat 注解

JsonFormat注解是jackson包里面的一個注解,需要加上依賴

<!-- 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注解需要用在實體類的時間字段上,對應的字段才能進行格式化。

import com.fasterxml.jackson.annotation.JsonFormat;import lombok.Data;import java.time.LocalDateTime;import java.util.Date;@Datapublic 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; }

如下所示

詳解Java關于時間格式化的方法

還有一種可以全局定義的

二、@JsonComponent 注解 (全局)

配置類

@JsonComponentpublic class DateFormatConfig { @Value('${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}') private String pattern; // date 類型全局時間格式化 @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 類型全局時間格式化 @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); }}

這樣我們就不用加注解了,也可以實現格式化

@JsonComponentpublic class DateFormatConfig { @Value('${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}') private String pattern; // date 類型全局時間格式化 @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 類型全局時間格式化 @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); }}

詳解Java關于時間格式化的方法

到此這篇關于詳解Java關于時間格式化的方法的文章就介紹到這了,更多相關Java 時間格式化內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 华蓥市| 施甸县| 皮山县| 乐亭县| 云霄县| 龙泉市| 遂溪县| 交城县| 淄博市| 清原| 罗江县| 英吉沙县| 临猗县| 平果县| 疏附县| 台南市| 乐安县| 沙湾县| 大石桥市| 怀远县| 察雅县| 永宁县| 太白县| 鸡西市| 柯坪县| 华蓥市| 崇义县| 突泉县| 绥滨县| 和龙市| 卓尼县| 沙河市| 沈阳市| 金塔县| 樟树市| 荔波县| 兴文县| 大新县| 博客| 云安县| 荔浦县|