java中计算Unix时间戳(timestamp)

unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

 

1,获取当前时间的timestamp

Date date = new Date();
        
long stamp = date.getTime()/1000;
        
System.out.println(stamp);
long timestamp = System.currentTimeMillis()/1000;
        
System.out.println(timestamp);

2,获取指定时间的timestamp

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-03-25 15:01:17");
        
long timestamp = date.getTime()/1000;
        
System.out.println(timestamp);

 

 

 

posted on 2020-03-27 17:36  Coder-Wf  阅读(9157)  评论(0)    收藏  举报

导航

< 2025年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12
点击右上角即可分享
微信分享提示