71

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.

2
  • 2
    You should really try to describe your problems better. Don't leave people to guess what it is that you're really trying to achieve. Commented Apr 16, 2012 at 16:02
  • Is this similar to an epoch, essentially a running value? Commented Jan 17, 2018 at 3:48

3 Answers 3

166
 long unixTime = System.currentTimeMillis() / 1000L; 
Sign up to request clarification or add additional context in comments.

2 Comments

L is redundant
As simple as it can be ☺
17

It's quite simple:

long timestamp = Calendar.getInstance().getTime() / 1000; 

2 Comments

Is this as good as System.currentTimeMillis() / 1000L ? (It looks less efficient to me - unless I am missing something?)
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
-2

Updated answer with Kotlin:

val unix = Calendar.getInstance().timeInMillis 

1 Comment

This answer is wrong. It should be Calendar.getInstance().timeInMillis / 1000. Check the other answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.