【報錯】Can not parse date while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS問題;保存後時間多8小時問題

【1】報錯信息:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.util.Date from String "2019-11-11 11:32:11": not a valid representation (error: Failed to parse Date value '2019-11-11 11:32:11': Can not parse date "2019-11-11 11:32:11": 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-11-11 11:32:11": not a valid representation (error: Failed to parse Date value '2019-11-11 11:32:11': Can not parse date "2019-11-11 11:32:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))
 at [Source: java.io.PushbackInputStream@116cf952; line: 1, column: 224] (through reference chain: net.jqsoft.scan.entity.MaternalExamGestation["examTime"])

解決方法:

在MaternalExamGestation實體中,定義examTime屬性的時候加入註解

 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

例如:

【2】但是加上這個註解後可能會遇到時區的問題

我前臺debug的時候,時間是2019-11-11 11:32:11

但是傳到後臺後,時間變成了2019-11-11 19:32:11,多了8個小時

如果遇到這種情況,就在@JsonFormat的註解中多加上timezone = "GMT+8"

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")

-----我是分割線--------------------------------我是分割線----------------------------------------我是分割線-----------------------------------------------

下面只是自己關於前臺後臺傳值的記錄,與上面的沒有什麼關係:

前端框架是mui

獲取當前時間

傳入後臺JS(以json形式傳到後臺去):

 doc.getElementById("theListCommit").addEventListener("tap", function() {  
var examTime =doc.getElementById("examTime").value;
//傳過去的是個實體,裏面還有其他的屬性要傳,暫時被我刪掉了,這裏只是部分代碼
 var maternalExamGestation={            
                    "examTime":examTime
                };
 var userId=doc.getElementById("theList_userId").value;
 var data = JSON.stringify(maternalExamGestation);
 $.ajax({
                    type: "POST",
                    url: "${ctx}/maternalExamGestation/saveOrUpdate?userId="+userId,
                    contentType:'application/json;charset=utf-8',
                    data:data,
                    dataType: "json"
                })
            });

後臺是接收maternalExamGestation這個實體對象

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章