简体   繁体   中英

Android DateTime to 11 digit Unix Timestamp

I need to get current date & time and convert it to 11 digit unix timestamp.

Just like this site does: http://www.onlineconversion.com/unix_time.htm

I googled a lot but all i could find was timestamp to date conversion problems & answers.

long unixTime = System.currentTimeMillis() / 1000L;

does what you want. Original answer here .

System.currentTimeMillis() returns the time from the Unix epoch in milliseconds. As 1 second = 1000 milliseconds , you simply divide it by 1000 to get the time in seconds.

您可以使用System.currentTimeMillis()获得以毫秒为单位的Unix时间戳,您可以将其转换为秒

long unixTimestamp = System.currentTimeMillis() / 1000L;

If you want to manipulate the date, you can use :

Date myDate = new Date();
myDate.setSeconds(23);
long uTime = myDate.getTime() / 1000L;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM