首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Java中将TimeStamp转换为Date?

如何在Java中将TimeStamp转换为Date?
EN

Stack Overflow用户
提问于 2012-08-07 12:25:01
回答 14查看 552.3K关注 0票数 114

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

我当前的代码如下:

代码语言:javascript
复制
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?

EN

回答 14

Stack Overflow用户

发布于 2012-08-07 12:29:12

只需使用图章的getTime()值作为参数创建一个新的Date对象。

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

代码语言:javascript
复制
Timestamp stamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(stamp.getTime());
System.out.println(date);
票数 206
EN

Stack Overflow用户

发布于 2013-04-05 22:51:11

代码语言:javascript
复制
// timestamp to Date
long timestamp = 5607059900000; //Example -> in ms
Date d = new Date(timestamp );

// Date to timestamp
long timestamp = d.getTime();

//If you want the current timestamp :
Calendar c = Calendar.getInstance();
long timestamp = c.getTimeInMillis();
票数 42
EN

Stack Overflow用户

发布于 2016-12-09 03:14:54

只要:

代码语言:javascript
复制
Timestamp timestamp = new Timestamp(long);
Date date = new Date(timestamp.getTime());
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11839246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档