element-plus 日期組件el-date-picker數據格式問題


使用element-plus的el-date-picker組件時發現默認情況下,組件接受並返回Date對象。

后台使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss'")進行參數綁定發現不能接收

Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2020-12-30T14:30:49.000Z'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-12-30T14:30:49.000Z]]

解決方法:

這個時候發現原因為匹配格式不正確,改為@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'.000Z'")

    @PostMapping("user/freeze")
    @PreAuthorize("hasAuthority('authority:user:freeze')")
    public ResponseEntity userFreeze(String username, @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'.000Z'") Date freezeDate) {
        userService.userFreeze(username, freezeDate);
        return ResponseEntity.success();
    }

於是就能正確接收了。

使用實體類綁定參數時,注解添加至字段上即可;請求content-type為application/json時可以使用@JsonFormat注解。

T是自定義的字符串,可以替換為其他任何的字符串。Z是表示時區,SSS是毫秒數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM