国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 11:51:53
字體:
供稿:網(wǎng)友

最近在做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("添加的預(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]]

看了控制臺的輸出信息,大概知道是前端將日期當做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默認使用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)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 波密县| 习水县| 广宁县| 闽侯县| 鱼台县| 太保市| 阿克陶县| 五大连池市| 鄂州市| 乡宁县| 阜新| 东台市| 涿鹿县| 龙川县| 桃源县| 罗城| 平遥县| 瑞昌市| 成都市| 拉萨市| 定西市| 临泽县| 独山县| 康定县| 博客| 昭苏县| 全州县| 西丰县| 伊通| 乌兰浩特市| 当涂县| 铜梁县| 石城县| 平阳县| 葵青区| 孟村| 桂东县| 灵宝市| 灌南县| 井研县| 阜南县|