This is the timestamp format I need: 2018-03-22 19:02:12.337909
7 Answers
Kotlin doesn't have any time handling classes of its own, so you just use Java's java.time
. For an ISO-8601 timestamp (which is the preferred format):
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
That will return 2018-04-16T17:00:08.746Z
. For your format, or if you need a different timezone, you can specify those:
DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")
.withZone(ZoneOffset.UTC)
.format(Instant.now())
See the java.time.format.DateTimeFormatter
JavaDoc for details on how to specify a format string.
The java.time classes are bundled with Android 26 and later, and with Java 8 and later. Most of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android (<26) in ThreeTenABP. See How to use ThreeTenABP….
Update 2020/07
The development of ThreeTenABP is winding down. With Gradle plugin 4.0 and higher, you can directly use java 8 APIs without requiring a minimum API level for your app.
For more information see Java 8+ API desugaring support (Android Gradle Plugin 4.0.0+)

- 697
- 1
- 7
- 13

- 17,207
- 15
- 66
- 82
-
9This requires API lvl 26 or above, any compatible alternative with older APIs? – DarkCygnus Jun 08 '18 at 17:59
-
1Oh yes, @DarkCygnus. For Android API level under 26 use the Android adaptation of backport of java.time. See [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. May 27 '19 at 18:55
val currentTimestamp = System.currentTimeMillis()

- 791
- 6
- 11
-
3This won't give you the format op requested. `System.currentTimeMillis()` returns a long, not a formatted string. – BDL Jul 05 '19 at 08:24
-
8maybe this is not the answer to this question, but when I search "timestamp in Kotlin" this is the best answer, so upvote – Azhagthott Dec 21 '20 at 05:24
-
1I don’t get how so many upvotes to this answer. Clearly the question said the required time stamp in the expected format. Which System.currentTimeMillis() does not return – Akash Verma Aug 26 '22 at 08:54
-
There are many kinds of timestamp, but (correctly or not), often the convention when using the term is to refer to a _numeric_ timestamp. That's probably why this answer is upvoted, because people just google "timestamp in Kotlin" and end up here – max pleaner Jan 05 '23 at 01:10
Try java.sql.Timestamp(System.currentTimeMillis())
i'm using API 19 and it works for me

- 3,678
- 3
- 36
- 48

- 594
- 1
- 5
- 11
-
1
-
2
-
12The terrible `java.sql.Timestamp` class was supplanted years ago by the *java.time* classes with the adoption of JSR 310. Replaced by `Instant` and `OffsetDateTime` classes. Suggesting the use of `Timestamp` in 2019 is poor advice. – Basil Bourque May 27 '19 at 20:28
First get the
val currentTimeMillis = System.currentTimeMillis()
// currentTimeMillis = 1674411796305
Then get TimeStamp like this
val timeStamp = Timestamp(currentTimeMillis)
// timestamp = 2023-01-22 20:23:16.305

- 31
- 2
package com.mkyong.date
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.Date
object TimeStampExample {
private val sdf = SimpleDateFormat("yyyy.MM.dd.HH.mm.ss")
@JvmStatic fun main(args:Array<String>) {
//method 1
val timestamp = Timestamp(System.currentTimeMillis())
println(timestamp)
//method 2 - via Date
val date = Date()
println(Timestamp(date.getTime()))
//return number of milliseconds since January 1, 1970, 00:00:00 GMT
println(timestamp.getTime())
//format timestamp
println(sdf.format(timestamp))
}
}

- 21
- 1
Use the kotlinx.datetime library. Here's how:
val currentTime = Clock.System.now()
println("The current time is $currentTime")
will print the current time in this format:
2023-02-28T07:43:04.618714Z

- 17,867
- 8
- 59
- 81
I'm using this
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
and here you have all Date and Time patterns https://developer.android.com/reference/java/text/SimpleDateFormat

- 123
- 2
- 8
-
6Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. May 27 '19 at 18:57
-
1Of course my bad for not mentioning Android Project use. I found this line of code in developer android documentation,it's simple and working for me so i thought it was fine. Sorry. – lev4 May 27 '19 at 21:54
-
3Please note that Kotlin is much more than Kotlin on Android. – treesAreEverywhere Jan 03 '20 at 17:00