詳解element-ui日期時間選擇器的日期格式化問題
最近在做vue+element-ui的后臺管理頁面,其中用到了DateTimePicker來選擇日期時間,但是在將數(shù)據(jù)傳回后臺的過程中遇到了一些令人頭疼的問題,在此記錄一下解決方案,以免日后再次遇到。

前端頁面
前端代碼
submitForm(formName) {
this.$refs[formName].validate((valid) => {
let url = 'http://localhost:8088/pethospital/order-record'
let data = qs.stringify({
title: this.orderForm.title,
hospitalId: this.orderForm.hospitalId,
orderDate: this.orderForm.orderDate,
orderType: this.orderForm.orderType,
petVariety: this.orderForm.petVariety,
mobilePhone: this.orderForm.mobilePhone,
supplement: this.orderForm.supplement
})
if (valid) {
axios.post(url, data)
.then(response => {
}).catch(error => {
this.$message({
message: '錯誤:' + error,
type: true
})
})
} else {
this.$message('驗證錯誤:請確認(rèn)信息是否填寫完整')
}
});
}
實體類代碼
private Long id; private String title; private Integer hospitalId; private Date orderDate; private Integer orderType; private String petVariety; private String mobilePhone; private String supplement;
Controller代碼
@PostMapping("/order-record")
public CommonResult addOrderRecord(OrderRecordDO orderRecordDO) throws ParseException {
System.out.println("添加的預(yù)約記錄:" + orderRecordDO);
orderRecordDOMapper.insertSelective(orderRecordDO);
return null;
}
控制臺輸出
Field error in object 'orderRecordDO' on field 'orderDate': rejected value [2019-04-10 10:00:00]; codes [typeMismatch.orderRecordDO.orderDate,typeMismatch.orderDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [orderRecordDO.orderDate,orderDate]; arguments []; default message [orderDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'orderDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2019-04-10 10:00:00'; nested exception is java.lang.IllegalArgumentException]]
看了控制臺的輸出信息,大概知道是前端將日期當(dāng)做String類型傳輸?shù)模俏覀兒笈_定義日期用的是Date類型,因此這里報的轉(zhuǎn)換異常。本來我想用SimpleDateFormat來轉(zhuǎn)換的,但是覺得這樣很麻煩,然后在網(wǎng)上查找相關(guān)資料發(fā)現(xiàn)可以有更簡單的方法。
嘗試1:
在實體類字段上添加@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date orderDate;
控制臺輸出
添加的預(yù)約記錄:{"id":null,"title":"測試1","hospitalId":1001,"orderDate":"Wed Apr 10 10:00:00 CST 2019","orderType":2001,"petVariety":"哈士奇","mobilePhone":"1000","supplement":"二哈"}
數(shù)據(jù)庫記錄

數(shù)據(jù)庫記錄
遇到的問題:從數(shù)據(jù)庫獲取數(shù)據(jù)后在前端顯示不友好

顯示
嘗試2: 在實體類字段添加@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")和@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
/** * timezone = "GMT+8"指定時區(qū) */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date orderDate;
前端顯示效果:這下就能顯示成我們想要的效果了

前端顯示
嘗試3:我的后臺項目使用SpringBoot搭建的,我在application.yml文件中添加如下配置
# 配置數(shù)據(jù)源 spring: datasource: name: pet-hospital type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/pet_hospital?serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 1741248769 # Vue前端傳來的日期為String類型,下面的設(shè)置可以自動將其轉(zhuǎn)換為Date類型,不需要手動轉(zhuǎn)換 mvc: date-format: yyyy-MM-dd HH:mm:ss # 以下設(shè)置可以將Date類型自動轉(zhuǎn)換為如下格式的日期,指定Jackson格式化日期使用的時區(qū),Jackson默認(rèn)使用UTC jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
顯示效果

顯示
總結(jié):
- 日期從前端傳到后端(添加),由String類型解析成Date類型,從后端傳到前端(查詢),由Date類型解析成String類型
- 可以使用注解的方式,@DateTimeFormat、@JsonFormat
- 可以使用配置文件方式,spring.mvc.date-format、spring.jackson.date-format/time-zone
- 為什么要設(shè)置time-zone?因為Jackson默認(rèn)使用UTC時區(qū),所以需要手動指定時區(qū)為GMT+8
附:原時間2019-04-12 12:00:00,相差8個小時

不指定時區(qū)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用electron轉(zhuǎn)換項目成桌面應(yīng)用方法介紹
Electron也可以快速地將你的網(wǎng)站打包成一個原生應(yīng)用發(fā)布,下面這篇文章主要給大家介紹了關(guān)于Vue和React中快速使用Electron的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
vue3 學(xué)習(xí)筆記之a(chǎn)xios的使用變化總結(jié)
本篇文章主要旨在幫助正在學(xué)vue3或者準(zhǔn)備學(xué)vue3的同學(xué)了解網(wǎng)絡(luò)請求axios該如何使用,防止接觸了一點點vue3的同學(xué)會有個疑問。有興趣的小伙伴可以關(guān)注一下2021-11-11
vue在install時node-sass@4.14.1?postinstall:node?scripts/buil
最近在npm install 的時候遇到了個問題,所以給大家總結(jié)下,下面這篇文章主要給大家介紹了關(guān)于vue在install時node-sass@4.14.1?postinstall:node?scripts/build.js錯誤的解決方法,需要的朋友可以參考下2023-05-05
Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn)方法
這篇文章主要介紹了Vue輸入框狀態(tài)切換&自動獲取輸入框焦點的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
Vue中使用crypto-js AES對稱加密算法實現(xiàn)加密解密
?在數(shù)字加密算法中,通過可劃分為對稱加密和非對稱加密,本文主要介紹了Vue中使用crypto-js AES對稱加密算法實現(xiàn)加密解密,文中根據(jù)實例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
elementui之封裝下載模板和導(dǎo)入文件組件方式
這篇文章主要介紹了關(guān)于elementui之封裝下載模板和導(dǎo)入文件組件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Vue-Router如何動態(tài)更改當(dāng)前頁url query
這篇文章主要介紹了Vue-Router如何動態(tài)更改當(dāng)前頁url query問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08

