@DateTimeFormat 转换报错

查看 78|回复 7
作者:cencoroll   
今天碰到一个情况就是 get 请求到后端的时候日期无法转换,但是 Date 上明明使用了注解但是就是报错
@DateTimeFormat(pattern = "yyyy-MM-dd")
//@JsonFormat(pattern = "yyyy-MM-dd" , timezone="GMT+8")
private Date date;
报错信息是这样的
code:400
data:{}
msg:
"date:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.alibaba.excel.annotation.format.DateTimeFormat java.util.Date] for value '2023-9-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-9-01]"
success:false
迷惑的点在于如果加上 HH:mm:ss 的话,把日期+00:00:00 就不会报错。
请教一下大佬到底怎么办才能让后端正确接收这个不带时分秒的日期?

DATE, type, failed, value

xianyv   
为啥你的 DateTimeFormat 的包名是 com.alibaba 的?不是 org.springframework.format.annotation 的吗
missz   
2023-9-01 换成 2023-09-01 试试
cencoroll
OP
  
@missz 都试过了,2023-9-1 2023-09-01 2023-9-01 这三个一样都是报错
@xianyv 这两个报名一样报错,都试过了,排查了一上午了
cencoroll
OP
  
@cencoroll 报名-->包名,基本把能排查的都排查了一遍了,就是说转换异常
```
date:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2023-09-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-09-01]
```
TinyBad   
用 LocalDate
oldshensheep   
2023 年了还有人在用 java.util.Date
换成 java.time.Instant
Huelse   
java.time.LocalDate
您需要登录后才可以回帖 登录 | 立即注册

返回顶部