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

前端頁(yè)面
前端代碼
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: '錯(cuò)誤:' + error,
type: true
})
})
} else {
this.$message('驗(yàn)證錯(cuò)誤:請(qǐng)確認(rèn)信息是否填寫完整')
}
});
}
實(shí)體類代碼
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;
}
控制臺(tái)輸出
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]]
看了控制臺(tái)的輸出信息,大概知道是前端將日期當(dāng)做String類型傳輸?shù)?,但是我們后臺(tái)定義日期用的是Date類型,因此這里報(bào)的轉(zhuǎn)換異常。本來(lái)我想用SimpleDateFormat來(lái)轉(zhuǎn)換的,但是覺(jué)得這樣很麻煩,然后在網(wǎng)上查找相關(guān)資料發(fā)現(xiàn)可以有更簡(jiǎn)單的方法。
嘗試1:
在實(shí)體類字段上添加@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date orderDate;
控制臺(tái)輸出
添加的預(yù)約記錄:{"id":null,"title":"測(cè)試1","hospitalId":1001,"orderDate":"Wed Apr 10 10:00:00 CST 2019","orderType":2001,"petVariety":"哈士奇","mobilePhone":"1000","supplement":"二哈"}
數(shù)據(jù)庫(kù)記錄

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

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

前端顯示
嘗試3:我的后臺(tái)項(xiàng)目使用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前端傳來(lái)的日期為String類型,下面的設(shè)置可以自動(dòng)將其轉(zhuǎn)換為Date類型,不需要手動(dòng)轉(zhuǎn)換 mvc: date-format: yyyy-MM-dd HH:mm:ss # 以下設(shè)置可以將Date類型自動(dòng)轉(zhuǎn)換為如下格式的日期,指定Jackson格式化日期使用的時(shí)區(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?因?yàn)镴ackson默認(rèn)使用UTC時(shí)區(qū),所以需要手動(dòng)指定時(shí)區(qū)為GMT+8
附:原時(shí)間2019-04-12 12:00:00,相差8個(gè)小時(shí)

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

