如何在 Java 中将时间戳转换为日期?

新手上路,请多包涵

在Java中获得计数后,如何将’timeStamp’转换为 date

我目前的代码如下:

 public class GetCurrentDateTime {

    public int data() {
        int count = 0;
        java.sql.Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
        java.sql.Date date = new java.sql.Date(timeStamp.getTime());
        System.out.println(date);
        //count++;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro", "root", "");

            PreparedStatement statement = con.prepareStatement("select * from orders where status='Q' AND date=CURDATE()");
            ResultSet result = statement.executeQuery();
            while (result.next()) {
                // Do something with the row returned.
                count++; //if the first col is a count.
            }
        } catch (Exception exc) {
            System.out.println(exc.getMessage());
        }

        return count;
    }
}

这是我的数据库:

在此处输入图像描述

这里我得到的输出是 2012-08-07 0,但等效查询返回 3。为什么我得到 0?

原文由 Krishna Veni 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 506
1 个回答

只需创建一个新的 Date 对象,将邮票的 getTime() 值作为参数。

这是一个示例(我使用当前时间的示例时间戳):

 Timestamp stamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(stamp.getTime());
System.out.println(date);

原文由 Alex Coleman 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题