詳解element-ui日期時(shí)間選擇器的日期格式化問題
最近在做vue+element-ui的后臺(tái)管理頁面,其中用到了DateTimePicker來選擇日期時(shí)間,但是在將數(shù)據(jù)傳回后臺(tái)的過程中遇到了一些令人頭疼的問題,在此記錄一下解決方案,以免日后再次遇到。
前端頁面
前端代碼
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)換異常。本來我想用SimpleDateFormat來轉(zhuǎn)換的,但是覺得這樣很麻煩,然后在網(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ù)庫記錄
數(shù)據(jù)庫記錄
遇到的問題:從數(shù)據(jù)庫獲取數(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前端傳來的日期為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中選中多個(gè)選項(xiàng)并且改變選中的樣式的實(shí)例代碼
這篇文章主要介紹了vue中選中多個(gè)選項(xiàng)并且改變選中的樣式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09vue arco.design錨點(diǎn)Anchor使用方式
這篇文章主要介紹了vue arco.design錨點(diǎn)Anchor使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04element-ui之關(guān)于組件BackToTop回到頂部的使用
這篇文章主要介紹了element-ui之關(guān)于組件BackToTop回到頂部的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue實(shí)現(xiàn)各種文件文檔下載及導(dǎo)出示例
這篇文章主要介紹了vue實(shí)現(xiàn)各種文件文檔下載及導(dǎo)出示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue-element-admin關(guān)閉eslint的校驗(yàn)方式
這篇文章主要介紹了vue-element-admin關(guān)閉eslint的校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue中正確使用Element-UI組件的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Vue中正確使用Element-UI組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10