Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current time as a TimeStamp in Kotlin?

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

like image 895
Amber Normand Avatar asked Apr 16 '18 16:04

Amber Normand


People also ask

How do I display the current time in Kotlin?

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.

What is timestamp in 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 .

How do you get time in milliseconds Kotlin?

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.") }


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

like image 67
Drew Stephens Avatar answered Oct 04 '22 21:10

Drew Stephens


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

like image 38
Andres Eusse Avatar answered Oct 04 '22 22:10

Andres Eusse


  val currentTimestamp = System.currentTimeMillis()
like image 38
Softlabsindia Avatar answered Oct 04 '22 22:10

Softlabsindia


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))
    }
}
like image 31
razko Avatar answered Oct 04 '22 22:10

razko