Avatar ·

How to get current timestamp in Android on Kotlin?

Hello everyone! I'm a beginner Android developer in Kotlin, and I have a simple but important problem. I need to get the current timestamp in my app, for example, to write it to a database or send it to a server. I know that in Java System.currentTimeMillis() is used, but in Kotlin I see many different ways and got confused.

\n\n

Here's what I tried:

\n\n
val timestamp1 = System.currentTimeMillis()\nval timestamp2 = Date().time\nval timestamp3 = Calendar.getInstance().timeInMillis\n
\n\n

All these options return some long numbers. But I'm not sure which one is correct for Android? Is there a difference between them? And is it possible to get the timestamp in seconds, not milliseconds? I heard about Insta