69

How do I create a Unix timestamp on Android?

I want to create a URL post request with a Unix timestamp on the end of the url.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40

3 Answers3

163
 long unixTime = System.currentTimeMillis() / 1000L;
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
15

It's quite simple:

long timestamp = Calendar.getInstance().getTime() / 1000;
mxcl
  • 26,392
  • 12
  • 99
  • 98
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 2
    Is this as good as `System.currentTimeMillis() / 1000L` ? (It looks less efficient to me - unless I am missing something?) – ban-geoengineering Aug 08 '14 at 09:38
  • 5
    For getting the current time, @Nesim has the better answer, but this answer is great if you already have a calendar object with a custom time. `myCalendar.getTimeInMillis() / 1000` – CyberEd Mar 01 '15 at 04:47
-1

Updated answer with Kotlin:

val unix = Calendar.getInstance().timeInMillis
Ricardo Yubal
  • 374
  • 3
  • 8