How to convert TimeStamp to DateTime in Kotlin?


Kotlin is a statistically typed language and it is based on Java, hence all the Java code can easily be compiled within Kotlin code. In this article, we will see how we can generate the current local date and time in Kotlin.

As Kotlin is interoperable with Java, we will be using Java utility class and Simple Date Format class in order to convert TimeStamp into DateTime.

Example – Converting DateTime using Java util class

As Kotlin is comparable with the JVM, we can use the Java util class in order to convert the TimeStamp into DateTime.

import java.text.SimpleDateFormat
import java.util.*

fun main(args: Array<String>) {
   val simpleDate = SimpleDateFormat("dd/M/yyyy hh:mm:ss")
   val currentDate = simpleDate.format(Date())
   println(" Current DateTime is -"+currentDate)
}

Output

On execution, it will print the current Date and Time.

Current DateTime is -28/2/2022 11:25:11

Updated on: 01-Mar-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements