- 如果前端向后端传递时间会报 nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.Column @org.springframework.format.annotation.DateTimeFormat java.util.Date],这是因为前后端格式不统一造成后端需要
@DateTimeFormat(pattern ="yyyy-MM-dd HH-mm-ss")
private Date startTime = new Date();
前端需要将传入的日期格式化成yyyy-MM-dd HH-mm-ss格式。
2.前端处理方式
通常我们可以为date添加格式化方法
Date.prototype.format = function (fmt) {
let o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"H+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(let k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
};
这样传值的时候使用new Date().format(“yyyy-MM-dd HH-mm-ss”);就可以了