Kotlin Get Current Timestamp Example

This Kotlin example shows how to create a timestamp in Kotlin.In this example, we create a timestamp format: 2018-03-22 19:02:12.337909.

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

Kotlin Get Current Timestamp Example

package net.javaguides.kotlin.examples

import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
fun main(args: Array<String>) {
 
    val formatter = DateTimeFormatter
    .ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")
    .withZone(ZoneOffset.UTC)
    .format(Instant.now())
 
    println("Current Date and Time is: $formatter")
}
Output:
Current Date and Time is: 2019-07-27 16:59:26.261000

Get Current Date and Time Example

package net.javaguides.kotlin.examples

import java.time.LocalDateTime

fun main(args: Array<String>) {
 val current = LocalDateTime.now()
 println("Current Date and Time is: $current")
}
Output:
Current Date and Time is: 2019-07-27T22:30:43.946


Comments