项目场景:
element-UI中2021-12-17T12:00:00.000Z 至 2022-01-17T12:00:00.000Z传入后台无法进行反序列化,于是筛选功能无法实现(我需要实现的功能)
问题描述:
前台
<span>收藏时间</span>
<el-date-picker
v-model="form.favoriteTime"
type="datetimerange"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="['12:00:00']">
</el-date-picker>
原因分析:
参考一:https://blog.csdn.net/weixin_31458459/article/details/113677700
参考二:https://blog.csdn.net/Murphy_six/article/details/119331627
解决方案:
方案一、
添加格式控制:value-format=“yyyy-MM-dd HH:mm:ss”,使前台传入的数据直接为 2021-12-17 12:00:00,后端格式化一次就好。
前端关键代码:
<span>收藏时间</span>
<el-date-picker
v-model="form.favoriteTime"
type="datetimerange"
value-format="yyyy-MM-dd HH:mm:ss"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="['12:00:00']">
</el-date-picker>``
Java关键代码:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp ts = new Timestamp(format.parse((String) object.get("time")).getTime());
方案二:
日期格式转换
2020-06-27T14:20:27.000000Z 时间格式转换成 2020-06-27 14:20:27
function rTime(date) {
var json_date = new Date(date).toJSON();
return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
let date = rTime('2020-06-27T14:20:27.000000Z');
console.log(date) // 2020-06-27 14:20:27
toJSON() 方法可以将 Date 对象转换为字符串,并格式化为 JSON 数据格式。