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.

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source