欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java日期時(shí)間格式化@JsonFormat與@DateTimeFormat的使用

 更新時(shí)間:2022年08月17日 15:47:20   作者:yujkss  
本文主要介紹了java日期時(shí)間格式化@JsonFormat與@DateTimeFormat的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

如果要使用 @JsonFormat 這個(gè)注解的話,需要在項(xiàng)目中添加 jackson 相關(guān)的依賴包;

因?yàn)?@JsonFormat 注解不是 Spring 自帶的注解,所以使用該注解前需要添加 jackson 相關(guān)的依賴包。當(dāng)然,如果是 SpringBoot 項(xiàng)目就不需要自己手動(dòng)添加依賴了,因?yàn)樵?spring-boot-start-web 下已經(jīng)包含了 jackson 相關(guān)依賴。

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.3</version>
</dependency>

如果要使用 @DateTimeFormat 這個(gè)注解的話,需要在項(xiàng)目中添加 springframework 相關(guān)的依賴包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.9</version>
</dependency>

大家可以去這個(gè)網(wǎng)站搜索想要的依賴:https://mvnrepository.com

@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”,timezone = “GMT+8”)
將后端返回給前端的日期時(shí)間進(jìn)行格式化,pattern為轉(zhuǎn)換后的格式,timezone為日期時(shí)間的時(shí)區(qū)
默認(rèn)情況下timeZone為GMT(即標(biāo)準(zhǔn)時(shí)區(qū)),而北京是在東八區(qū),所以會(huì)造成差8小時(shí)。
提示:@JsonFormat注解可以在屬性的上方,同樣可以在屬性對(duì)應(yīng)的get方法上,兩種方式?jīng)]有區(qū)別

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime userCreateDate;

@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
將前端傳給后端的日期時(shí)間進(jìn)行格式化,pattern為轉(zhuǎn)換后的格式

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime userCreateDate;

POST請(qǐng)求,我們一般會(huì)用@RequestBody接收J(rèn)SON對(duì)象,如果對(duì)象里面有日期時(shí)間類型數(shù)據(jù)的話,我們可以使用 @JsonFormat注解進(jìn)行格式化,它既可以對(duì)出參進(jìn)行格式化,也可以對(duì)入?yún)⑦M(jìn)行格式化

GET請(qǐng)求參數(shù)都是拼接在URL后面的,則需要使用@DateTimeFormat對(duì)入?yún)⑦M(jìn)行格式化,放到@RequestBody修飾的對(duì)象里面是無(wú)效的

@JsonFormat是格式化json格式的。
@DateTimeFormat是格式化 key=value 這種格式的。

需要取數(shù)據(jù)到前臺(tái),也需要前臺(tái)數(shù)據(jù)傳到后臺(tái),都需要進(jìn)行時(shí)間格式的轉(zhuǎn)換,可以同時(shí)使用

  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
  private Date symstarttime;

SpringBoot的配置文件

application.yml

spring:
  # Content-Type 為 application/json, @JsonFormat(優(yōu)先級(jí)高) 或 spring.jackson.date-format
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  # Content-Type 為 application/x-www-form-urlencoded(普通表單上傳)spring.mvc.date-format(優(yōu)先級(jí)高) 或 @DatetimeFormat
  mvc:
    format:
      date-time: yyyy-MM-dd HH:mm:ss

application.properties

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.serialization.indent_output=true
spring.jackson.serialization.fail_on_empty_beans=false

到此這篇關(guān)于java日期時(shí)間格式化@JsonFormat與@DateTimeFormat的使用的文章就介紹到這了,更多相關(guān)java @JsonFormat與@DateTimeFormat內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論