This is the timestamp format I need: 2018-03-22 19:02:12.337909
To get local formatting use getDateInstance() , getDateTimeInstance() , or getTimeInstance() , or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
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 .
fun main(args: Array<String>) { val milliseconds: Long = 1000000 val minutes = milliseconds / 1000 / 60 val seconds = milliseconds / 1000 % 60 println("$milliseconds Milliseconds = $minutes minutes and $seconds seconds.") }
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+)
Try java.sql.Timestamp(System.currentTimeMillis())
i'm using API 19 and it works for me
val currentTimestamp = System.currentTimeMillis()
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))
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With