Java前后端時(shí)間格式的轉(zhuǎn)化方式
JsonFormat、DateTimeFormat使用
從數(shù)據(jù)庫獲取時(shí)間傳到前端進(jìn)行展示的時(shí)候,我們有時(shí)候可能無法得到一個(gè)滿意的時(shí)間格式的時(shí)間日期,在數(shù)據(jù)庫中顯示的是正確的時(shí)間格式,獲取出來卻變成了很丑的時(shí)間戳,@JsonFormat注解很好的解決了這個(gè)問題,我們通過使用@JsonFormat可以很好的解決:后臺到前臺時(shí)間格式保持一致的問題。
其次,另一個(gè)問題是,我們在使用WEB服務(wù)的時(shí),可能會(huì)需要用到,傳入時(shí)間給后臺,比如注冊新用戶需要填入出生日期等,這個(gè)時(shí)候前臺傳遞給后臺的時(shí)間格式同樣是不一致的,而我們的與之對應(yīng)的便有了另一個(gè)注解,@DataTimeFormat便很好的解決了這個(gè)問題,接下來記錄一下具體的@JsonFormat與DateTimeFormat的使用過程。
@JsonFormat
導(dǎo)入依賴
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
在你需要查詢出來的時(shí)間的數(shù)據(jù)庫字段對應(yīng)的實(shí)體類的屬性上添加 @JsonFormat
@Data @Api("升級日志返回值") public class UpgradeLogRes { @ApiModelProperty("升級日志id") private Long id; @ApiModelProperty("版本名稱") private String name; @ApiModelProperty("發(fā)布日期") @JsonFormat(pattern = "yyyy-MM-dd") private Date postDate; @ApiModelProperty("內(nèi)容日志") private String contentLog; @ApiModelProperty("發(fā)布狀態(tài)") private Integer postStatus; @ApiModelProperty("內(nèi)容日志Url") private String contentLogUrl; }
注:@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)
pattern:是你需要轉(zhuǎn)換的時(shí)間日期的格式
timezone:是時(shí)間設(shè)置為東八區(qū),避免時(shí)間在轉(zhuǎn)換中有誤差
提示:@JsonFormat注解可以在屬性的上方,同樣可以在屬性對應(yīng)的get方法上,兩種方式?jīng)]有區(qū)別
@DateTimeFormat
導(dǎo)入依賴:@DateTimeFormat的使用和@jsonFormat差不多,首先需要引入是spring還有jodatime,spring我就不貼了
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.3</version> </dependency>
在controller層我們使用spring mvc 表單自動(dòng)封裝映射對象時(shí),我們在對應(yīng)的接收前臺數(shù)據(jù)的對象的屬性上加@DateTimeFormat
@DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date symstarttime; @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date symendtime;
我這里就只貼這兩個(gè)屬性了,這里我兩個(gè)注解都同時(shí)使用了,因?yàn)槲壹刃枰?shù)據(jù)到前臺,也需要前臺數(shù)據(jù)傳到后臺,都需要進(jìn)行時(shí)間格式的轉(zhuǎn)換,可以同時(shí)使用
總結(jié)一下:
- 注解@JsonFormat主要是后臺到前臺的時(shí)間格式的轉(zhuǎn)換
- 注解@DataFormAT主要是前后到后臺的時(shí)間格式的轉(zhuǎn)換
java前后端Date接收
1.前端傳Date對象
將其轉(zhuǎn)為“yyyy-MM-dd HH:mm:ss”的字符串,后臺用@DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”)格式化Date屬性
2.后臺返回給前端Date
傳的是時(shí)間戳,用@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”, timezone = “GMT+8”)對其格式化,
timezone是用于調(diào)整時(shí)區(qū)的屬性(東八區(qū)),不加的話得到的時(shí)間會(huì)比實(shí)際的少8個(gè)小時(shí)
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date startTime;
3.時(shí)間比較:
mybaties :
startTime, endTime是經(jīng)過@DateTimeFormat格式后的Date對象
<if test="startTime != null"> and alarm.createTime >= #{startTime} </if> <if test="endTime != null"> and alarm.createTime <= #{endTime} </if>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 詳解Java關(guān)于時(shí)間格式化的方法
- java 格式化時(shí)間的示例代碼
- Java驗(yàn)證時(shí)間格式是否正確方法類項(xiàng)目實(shí)戰(zhàn)
- Java新API的時(shí)間格式化
- java中的實(shí)體類時(shí)間格式化
- java如何讓帶T的時(shí)間格式化
- 學(xué)習(xí)Java之如何對時(shí)間進(jìn)行格式化
- Java格式化日期和時(shí)間三種方法
- mysql時(shí)間格式和Java時(shí)間格式的對應(yīng)方式
- Java處理時(shí)間格式CST和GMT轉(zhuǎn)換方法示例
- JAVA獲取特定格式時(shí)間方式
相關(guān)文章
學(xué)習(xí)spring事務(wù)與消息隊(duì)列
這篇文章主要為大家詳細(xì)介紹了spring事務(wù)與消息隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10利用微信小程序+JAVA實(shí)現(xiàn)微信支付的全過程
微信支付是一種在線支付解決方案,允許用戶通過微信內(nèi)的支付功能進(jìn)行付款,下面這篇文章主要給大家介紹了關(guān)于利用微信小程序+JAVA實(shí)現(xiàn)微信支付的相關(guān)資料,需要的朋友可以參考下2024-08-08關(guān)于MyBatis 查詢數(shù)據(jù)時(shí)屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù))
這篇文章主要介紹了MyBatis 查詢數(shù)據(jù)時(shí)屬性中多對一的問題(多條數(shù)據(jù)對應(yīng)一條數(shù)據(jù)),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Servlet和Filter之間的區(qū)別與聯(lián)系
這篇文章主要介紹了Servlet和Filter之間的區(qū)別與聯(lián)系的相關(guān)資料,需要的朋友可以參考下2016-05-05