如何在数据库中插入时间戳? [英] how to insert Timestamp in database?

查看:2154
本文介绍了如何在数据库中插入时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

java.lang.IllegalArgumentException:时间戳记格式必须为yyyy-mm-dd hh:mm:ss [.fffffffff]

java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

这是我的Java代码:

Here is my java code:

<%  int reg;

        String sdate = request.getParameter("sdate");
        //Timestamp ts=request.get
      //out.println(sdate);
         String text = "0000-00-00 00:00:00";
       Timestamp ts = Timestamp.valueOf(text);
       // Timestamp ts1=Timestamp.valueOf(sdate);
       //  Timestamp ts2=Timestamp.valueOf(edate);
    try{
          String s;
      Format formatter;
     Date date = new Date(sdate);
     //out.println(date);
      formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
     s = formatter.format(date);
     out.println(s);
        PreparedStatement ps=cn.prepareStatement("insert into c1 values(?)");
                           // ps.setString(1,ts);
         ps.setTimestamp(1,ts);



                            int i=ps.executeUpdate();
                            if(i==1)
                        {
                       pw.println("record inserted");
                        }
                        else
                        {
                        pw.println("not iNSERTED");
                        }
                        //cn.close();

                       }
                 catch(Exception e)
                        {
                            out.println(e.toString());
                        }

    %>

有什么建议吗?

推荐答案

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的形式发送.

这篇关于如何在数据库中插入时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆