current timestamp kotlin code example

Example: get timestamp kotlin

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.