ResultSet中的getdate只能取到日期不能取到时间??

来源:互联网 发布:香港电信网络制式 编辑:程序博客网 时间:2024/04/29 20:28

rs.getDate()只是返回日期部分

rs.getTime()只是返回时间部分

rs.getTimestamp()才是返回时间和日期


今天碰到一个mysql日期时间比较查询的问题,将解决的经验写下来分享。

    mysql数据库中存的时间格式为2008-12-28 18:08:08,现在先要从一个一个结果集rs中获得一个日期时间。我先用rs.getDate()方法试着获取时间,结果只有年月日,无法获取小时、分和秒。最后解决的方法是:

      Date time1=new Date(rs.getTimestamp("pub_time").getTime());
      SimpleDateFormat formattime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String pubtime=formatime.format(time1);

    获得的pubtime为String型,在sql语句中用mysql的时间函数date_format('time','format')转换:

    String sqlstr="select * from do_document where pub_time<date_format('"+pubtime+"','%Y-%m-%d %H:%i:%s') order by pub_time desc limit 0,1";

    然后执行该sql语句就查到了满足条件的记录。


原创粉丝点击