0

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.

Max Abrahamsson
  • 1,460
  • 3
  • 17
  • 35

3 Answers3

3
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.

Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
2

You can use System.currentTimeMillis() to get the Unix timestamp in milliseconds which you can convert to seconds

long unixTimestamp = System.currentTimeMillis() / 1000L;
Suhail Patel
  • 13,644
  • 2
  • 44
  • 49
2

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

Date myDate = new Date();
myDate.setSeconds(23);
long uTime = myDate.getTime() / 1000L;
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54