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

在获得java中的计数后,如何将“timeStamp”转换为date

我目前的代码如下:

公共类GetCurrentDateTime{
公共int数据(){
整数计数=0;
java.sql.Timestamp Timestamp=新的时间戳(System.currentTimeMillis());
java.sql.Date-Date=new java.sql.Date(timeStamp.getTime());
系统输出打印项次(日期);
//计数++;
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/pro“,”根“,”);
PreparedStatement=con.prepareStatement(“从状态为Q且日期为CURDATE()的订单中选择*”;
ResultSet result=statement.executeQuery();
while(result.next()){
//对返回的行执行某些操作。
count++;//如果第一列是count。
}
}捕获(异常exc){
System.out.println(exc.getMessage());
}
返回计数;
}
}

这是我的数据库:

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

只需使用戳记的getTime()值作为参数,创建一个新的Date对象

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

时间戳=新的时间戳(System.currentTimeMillis());
日期日期=新日期(stamp.getTime());
系统输出打印项次(日期);

发表评论