java timestamp 转date_如何在Java中将TimeStamp转换为Date?

问题

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

}

}

这是我的数据库:

2d319098-7e11-49f4-9db3-65df4e44db82.png

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

#1 热门回答(152 赞)

只需创建一个带有标记为getTime()value的newDate对象作为参数。

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

Timestamp stamp = new Timestamp(System.currentTimeMillis());

Date date = new Date(stamp.getTime());

System.out.println(date);

#2 热门回答(33 赞)

// 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();

#3 热门回答(10 赞)

只是:

Timestamp timestamp = new Timestamp(long);

Date date = new Date(timestamp.getTime());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值