如何在 Java 中将时间戳转换为日期? [英] How to convert TimeStamp to Date in Java?

查看:107
本文介绍了如何在 Java 中将时间戳转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中获取计数后如何将'timeStamp'转换为date?

How do I convert 'timeStamp' to date after I get the count in java?

我目前的代码如下:

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?

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?

推荐答案

只需创建一个新的 Date 对象,并将图章的 getTime() 值作为参数.

Just make a new Date object with the stamp's getTime() value as a parameter.

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

Here's an example (I use an example timestamp of the current time):

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

这篇关于如何在 Java 中将时间戳转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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