SpringBoot三種方法接口返回日期格式化小結(jié)
方法一:@JsonFormat注解
在返回實(shí)體的字段上添加@JsonFormat注解。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createDate;
pattern是日期格式,timezone是時(shí)間分區(qū)。格式具體可以參考下圖:
方法二:JsonConfig配置
全局配置。好處:無需每個(gè)類配置日期格式壞處:不靈活,只能統(tǒng)一。
package com.xy.config; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.boot.jackson.JsonComponent; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.TimeZone; //jackson日期 @Slf4j @JsonComponent public class JacksonConfig { @Value("${my.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() { return builder -> { TimeZone tz = TimeZone.getTimeZone("GMT+8"); DateFormat df = new SimpleDateFormat(pattern); df.setTimeZone(tz); builder.failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df); }; } @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }
然后配置文件里配置好格式就行。
方法三:yml配置
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
UTC和 GMT區(qū)別UTC:協(xié)調(diào)世界時(shí)間(UTC)是基于原子時(shí)鐘的時(shí)間計(jì)量系統(tǒng),旨在盡量接近世界時(shí)(UT)。UTC的時(shí)間尺度是均勻的,不考慮地球自轉(zhuǎn)速度的變化。GMT+8:格林威治平均時(shí)間加8小時(shí),即東八區(qū)的本地時(shí)間。GMT+8通常用于表示中國北京時(shí)間UTC:在國際無線電通信、衛(wèi)星導(dǎo)航等需要高精度時(shí)間計(jì)量的場合廣泛使用。GMT+8:常用于表示中國北京時(shí)間,在電子郵件信頭、軟件顯示時(shí)間等場合使用??偨Y(jié):UTC和GMT+8基本相同,UTC更精確而GMT+8常代表北京時(shí)間。
總結(jié)
三種方法個(gè)人推薦第三種。為什么,因?yàn)樘奖懔耍硗馊绻刑厥飧袷?,可以再加@JsonFormat單獨(dú)注解,會優(yōu)先以添加了@JsonFormat注解的為準(zhǔn)。
到此這篇關(guān)于SpringBoot三種方法接口返回日期格式化小結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot 接口返回日期格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring AOP的底層實(shí)現(xiàn)方式-代理模式
這篇文章主要介紹了Spring AOP的底層實(shí)現(xiàn)方式-代理模式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11詳解如何在Spring Boot啟動后執(zhí)行指定代碼
這篇文章主要介紹了在Spring Boot啟動后執(zhí)行指定代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06IntelliJ IDEA中properties文件顯示亂碼問題的解決辦法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中properties文件顯示亂碼問題的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10Java實(shí)戰(zhàn)之小蜜蜂擴(kuò)音器網(wǎng)上商城系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)簡單的小蜜蜂擴(kuò)音器網(wǎng)上商城系統(tǒng),文中采用到的技術(shù)有JSP、Servlet?、JDBC、Ajax等,感興趣的可以動手試一試2022-03-03