46

This is the timestamp format I need: 2018-03-22 19:02:12.337909

Amber Normand
  • 492
  • 1
  • 5
  • 10

5 Answers5

47

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+)

Milad
  • 324
  • 3
  • 9
Drew Stephens
  • 16,317
  • 14
  • 63
  • 81
  • 8
    This requires API lvl 26 or above, any compatible alternative with older APIs? – DarkCygnus Jun 08 '18 at 17:59
  • 1
    Oh 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
37

Try java.sql.Timestamp(System.currentTimeMillis()) i'm using API 19 and it works for me

Sayed Abolfazl Fatemi
  • 3,384
  • 2
  • 32
  • 47
Andres Eusse
  • 546
  • 1
  • 5
  • 11
27
  val currentTimestamp = System.currentTimeMillis()
Softlabsindia
  • 533
  • 5
  • 11
  • 1
    This won't give you the format op requested. `System.currentTimeMillis()` returns a long, not a formatted string. – BDL Jul 05 '19 at 08:24
  • 3
    maybe 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
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))
    }
}
razko
  • 21
  • 1
0

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

lev4
  • 115
  • 2
  • 8
  • 4
    Please 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
  • Of 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
  • 3
    Please note that Kotlin is much more than Kotlin on Android. – treesAreEverywhere Jan 03 '20 at 17:00