【报错】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这个实体对象

 

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