Java里面timestamp和long的相互转换

Posted on 2013-01-11 10:49  炫谷  阅读(44611)  评论(0编辑  收藏  举报
long t = System.currentTimeMillis();// 获得当前系统毫秒数,这个是1970-01-01到现在的毫秒数
System.out.println("t = "+t);
Date da = new Date(t);
System.out.println(
"da = "+da); long currentTime = da.getTime(); // 即时毫秒数 System.out.println("currentTime = "+currentTime); System.out.println("currentTime/1000 = "+currentTime/1000);// 秒数

运行结果:

t = 1357872222592 //毫秒数
da = 2013-01-11
currentTime = 1357872222592 //毫秒数,currentTime = t
currentTime/1000 = 1357872222  //秒

Timestamp scurrtest = new Timestamp(System.currentTimeMillis());
System.out.println("scurrtest = "+scurrtest);
long sqlLastTime = scurrtest.getTime();// 直接转换成long
System.out.println("sqlLastTime = "+sqlLastTime); //毫秒数
System.out.println("sqlLastTime/1000 = "+sqlLastTime/1000);     // 秒数

运行结果:

scurrtest = 2013-01-11 10:43:42.598
sqlLastTime = 1357872222598
sqlLastTime/1000 = 1357872222  //

Copyright © 2024 炫谷
Powered by .NET 9.0 on Kubernetes