Get date representation in seconds?

这一生的挚爱 提交于 2019-11-29 03:41:20

import java.util.Date;

... long secs = (new Date().getTime())/1000; ...

Please see - http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()

java.util.Date.getTime() it returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

    java.util.Date date=new Date();
    System.out.println(date.getTime());

Output: 1340128712111

Number of seconds by itself doesn't mean much. Number of seconds within the current minute? Number of seconds since 0:00:00 Janurary 1st, 1970? Number of seconds since lunch? Could you be more specific.

Put it into the API also doesn't mean much, unless you specify exactly which API you are using, and where you are attempting to put these seconds.

Since Java 8 and onwards there's this elegant method which returns the Epoch time in seconds (seconds since 0:00:0 January 1st 1970). You can then store this value as an numeric value: a "long" in this case.

long timestamp = java.time.Instant.now().getEpochSecond();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!