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

詳解element-ui日期時間選擇器的日期格式化問題

 更新時間:2019年04月08日 09:57:01   作者:codecho  
這篇文章主要介紹了詳解element-ui日期時間選擇器的日期格式化問題,本文用到了DateTimePicker來選擇日期時間,但是在將數(shù)據(jù)傳回后臺的過程中遇到了一些令人頭疼的問題,有興趣的一起來了解一下

最近在做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('驗證錯誤:請確認信息是否填寫完整')
          }
        });
      }

實體類代碼

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("添加的預約記錄:" + 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]]

看了控制臺的輸出信息,大概知道是前端將日期當做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;

控制臺輸出

添加的預約記錄:{"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默認使用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默認使用UTC時區(qū),所以需要手動指定時區(qū)為GMT+8

附:原時間2019-04-12 12:00:00,相差8個小時

不指定時區(qū)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue中選中多個選項并且改變選中的樣式的實例代碼

    vue中選中多個選項并且改變選中的樣式的實例代碼

    這篇文章主要介紹了vue中選中多個選項并且改變選中的樣式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • vue Antd 輸入框Input自動聚焦方式

    vue Antd 輸入框Input自動聚焦方式

    這篇文章主要介紹了vue Antd 輸入框Input自動聚焦方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue arco.design錨點Anchor使用方式

    vue arco.design錨點Anchor使用方式

    這篇文章主要介紹了vue arco.design錨點Anchor使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Vue條件判斷之循環(huán)舉例詳解

    Vue條件判斷之循環(huán)舉例詳解

    在Vue進行前端開發(fā)中,條件判斷主要用于根據(jù)不同的條件來決定顯示或隱藏,或者進行視圖之間的切換,這篇文章主要給大家介紹了關(guān)于Vue條件判斷之循環(huán)舉例詳解的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • Vue.directive()的用法和實例詳解

    Vue.directive()的用法和實例詳解

    這篇文章主要介紹了Vue.directive()的用法和實例 ,需要的朋友可以參考下
    2018-03-03
  • element-ui之關(guān)于組件BackToTop回到頂部的使用

    element-ui之關(guān)于組件BackToTop回到頂部的使用

    這篇文章主要介紹了element-ui之關(guān)于組件BackToTop回到頂部的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vuex中狀態(tài)管理器的使用詳解

    Vuex中狀態(tài)管理器的使用詳解

    這篇文章主要介紹了Vuex狀態(tài)管理器的使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • vue實現(xiàn)各種文件文檔下載及導出示例

    vue實現(xiàn)各種文件文檔下載及導出示例

    這篇文章主要介紹了vue實現(xiàn)各種文件文檔下載及導出示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • vue-element-admin關(guān)閉eslint的校驗方式

    vue-element-admin關(guān)閉eslint的校驗方式

    這篇文章主要介紹了vue-element-admin關(guān)閉eslint的校驗方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue中正確使用Element-UI組件的方法實例

    Vue中正確使用Element-UI組件的方法實例

    這篇文章主要給大家介紹了關(guān)于Vue中正確使用Element-UI組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10

最新評論