今天和前端聯(lián)調(diào)接口遇到一個關(guān)于時間格式的問題
問題描述
前端給的入?yún)ⅲ?/h6>
入?yún)?png
后臺定義的DTO:

入?yún)?png
時間格式給的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:
選擇時區(qū)
@JsonFormat(pattern = "yyyy-MM-dd",locale = "zh",timezone="GMT+8")