如何在Java中将TimeStamp转换为Date? [英] How to convert TimeStamp to Date in Java?

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

问题描述

$ p

我当前的目标是在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);
连接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()){
//对返回的行执行某些操作。
count ++; //如果第一列是计数。
}
} catch(Exception exc){
System.out.println(exc.getMessage());
}

返回计数;
}
}

这是我的数据库:



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

解决方案

只需创建一个新的Date对象邮票的getTime值作为参数



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

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


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

My current code is as follows:

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;
    }
}

This is my database:

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

解决方案

Just make a new Date object with the stamp's getTime value as a paramater

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中将TimeStamp转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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