后台接收参数:
{"taskType":1,"billDate":"2020-11-19 00:00:00"}
方法1:
import org.springframework.format.annotation.DateTimeFormat;
/**
* 账单时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date billDate;
结果-格式化失败:
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2020-11-19 00:00:00": not a valid representation (error: Failed to parse Date value '2020-11-19 00:00:00': Cannot parse date "2020-11-19 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
更改为提示格式@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ") 仍旧失败,放弃。
原因:https://my.oschina.net/u/3694704/blog/2243415
根本原因是日期格式化转换异常,文章博主重写了格式化部分代码,未亲测,有可行性。但是不确定其它形式日期格式是否兼容,以及不同版本对其它模块是否造成蝴蝶效应,不推荐使用方法1。
方法2:
后台接收(未验证,仅记录):
import com.fasterxml.jackson.annotation.JsonFormat;
//JsonFormat会让时间以0区时间显示,记得更新时区
@JsonFormat(pattern="yyyy-MM-dd HH:mm:SS",timezone="GMT+8")
private String billDate;
结果:正常
依赖包:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>