Spring Boot 时间格式问题
2019-06-11 本文已影响0人
可乐zzz
今天和前端联调接口遇到一个关于时间格式的问题
问题描述
前端给的入参:

后台定义的DTO:
时间格式给的Date
private Date repayTimeMin;
private Date repayTimeMax;
报错:
{"msg":"JSON parse error: Can not deserialize value of type java.util.Date from String \"2019-06-05 00:00\": not a valid representation (error: Failed to parse Date value '2019-06-05 00:00': Can not parse date \"2019-06-05 00:00\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String \"2019-06-05 00:00\": not a valid representation (error: Failed to parse Date value '2019-06-05 00:00': Can not parse date \"2019-06-05 00:00\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))\n at [Source: java.io.PushbackInputStream@3f8b2dd6; line: 1, column: 87] (through reference chain: com.github.collection.admin.model.dto.PreCaseDataPoolQueryDTO[\"repayTimeMin\"])","code":500,"data":null}
解决方案:
在字段前加上注解即可
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private Date repayTimeMin;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
private Date repayTimeMax;
tips:
选择时区
@JsonFormat(pattern = "yyyy-MM-dd",locale = "zh",timezone="GMT+8")