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是毫秒数

posted @ 2020-12-30 14:57  向零  阅读(5875)  评论(1编辑  收藏  举报