借鉴的文章.
本次的处理 可以说是东拼西凑的结果,后期考虑优化,本次先做记录
前台的处理
显示时间戳的控件
使用的是input 类型选择的是 datetime-local
这个控件有个弊端 没有 秒显示的区域
1)拿到控件的值
let saledateVal=$("#saledate").val();
- 对拿到的值做处理
调用方法 目的是分离得到时间戳前面的时间部分
原因是
let saledateVal=$("#saledate").val();
拿到的数据的型为
2020-12-19T14:25
不是想要的结果,所以需要自己构造,调用下的方法,构造成 2020-12-19 14:25:00 形式
function formatterDatetimeLocalToAppricationMy(dateMy) {
let n=dateMy.split("T");
let dateNeed=n[0]+" "+n[1]+":00";
return dateNeed;
}
3)构造成功之后,利用ajax携带数据到后台即可
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat @com.fasterxml.jackson.annotation.JsonFormat java.sql.Timestamp] for value ‘2020-12-18 22:25:00’; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.Date] to type [@org.springframework.format.annotation.DateTimeFormat @com.fasterxml.jackson.annotation.JsonFormat java.sql.Timestamp]]]
上面的意思大概说是json转换字符串的时候出了问题,无法转换 String类型和 TimeStamp 类型
这个错误出现的原因是因为笔者在实体类的timestamp字段上加了注解,去掉之后就好了
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Timestamp saledate;
这样转换的时候出了问题,这两个注解之前处理 Date格式的时候屡试不爽
处理Date格式的是用法如下
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
private Date saledate;
这样这个日期字段使用上面就和String类似,不需要前后台类型转换,本来想套用的,但是好像套用失败了。。这个可以后期优化,欢迎提出针对 TimeStamp 的注解的加法,请评论回复,谢谢
数据库建表的时候,建议添加 timestamp 字段的默认值添加默认值,当然,需要根据具体的业务逻辑来处理,笔者为了方便插入数据的时候的麻烦,就为类型是 timestamp 的字段添加了默认值