vue向spring boot传递时间问题
·
- 如果前端向后端传递时间会报 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”);就可以了
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)