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  阅读(8928)  评论(0编辑  收藏  举报

导航