将时间写入数据库时,秒带小数点问题

1.问题原因:new Date() 时间秒带小数点

2.解决思路:将new Date() 格式化为年月日时分秒字符串,在将该字符串转为Date.

3.示例

Constant类

public final static SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

 ServiceImpl类

...
entity.setCreateTime(Constant.DF.parse(Constant.DF.format(new Date())));
Dao.insert(entity);