且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数据库中插入时间戳?

更新时间:2023-01-22 12:53:13

Shakti,您需要解决该错误. java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

Shakti, you need to address the error. java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

,然后查看格式化日期的方式. 您需要在下面更改行

and then have a look at the way you've formatted your date. You'll need to change your line below

  formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

收件人:

 formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

请注意在日期中使用hypen字符而不是您使用的冒号字符的区别和用途.

Notice the difference and use of the hypen character in the date instead of the colon character which youre using.

更新1

好的.因此,在@sotirios发表评论后,我决定做一些额外的研究,发现这篇文章并学到了一些东西.您不能提供0000-00 ...日期配置,因为MySQL [使用零日期"作为特殊占位符

Ok. so after @sotirios' comment, I decided to do some extra research and I came across this post and learned something. You cant give a 0000-00... date configuration because MySQL [uses "zero dates" as special placeholder

简单明了的解决方案是将日期字符串更改为

The solution plain and simple is to change the date string from

String text = "0000-00-00 00:00:00";

至少

String text = "0001-01-01 00:00:00";

如果您真的要发送0000-00 -...日期字符串,我认为解决方案是将"zeroDateTimeBehavior = convertToNull"指定为MySQL连接的参数(在数据源URL中或作为其他参数)属性),例如:

jdbc:mysql://localhost/myDatabase?zeroDateTimeBehavior=convertToNull

这将导致所有此类值以NULL的形式发送.