詳解Java關(guān)于時(shí)間格式化的方法
一般從數(shù)據(jù)庫(kù)獲取的時(shí)間或日期時(shí)間格式化為date或者datetime,為了方便前端渲染,API接口返回的時(shí)候需要對(duì)日期進(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ā)了。
接下來(lái)介紹SpringBoot中常用的對(duì)時(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í)間字段上,對(duì)應(yīng)的字段才能進(jìn)行格式化。
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; }
如下所示
還有一種可以全局定義的
二、@JsonComponent 注解 (全局)
配置類
@JsonComponentpublic 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)格式化
@JsonComponentpublic 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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法2. WMLScript的語(yǔ)法基礎(chǔ)3. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……4. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁(yè)的問(wèn)題5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. xml中的空格之完全解說(shuō)7. XML入門的常見(jiàn)問(wèn)題(四)8. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)9. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法10. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享
