Epoch and Date Time Conversion in Kotlin

Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. It is a modern programming language that makes developers happier. It provides many date time functions to handle date time functionality.

Here we will explain Kotlin date time functions to get current epoch or Unix timestamp, convert timestamp to date and convert date to epoch or Unix timestamp.

Get current epoch or Unix timestamp in Kotlin

You can get the current unix timestamp using currentTimeMillis().

System.currentTimeMillis()

Convert epoch or Unix timestamp to date in Kotlin

You can convert unix timestamp to date as follows.

val timeStamp = Timestamp(System.currentTimeMillis()) val date = Date(timeStamp.getTime())

Convert date to epoch or unix timestamp in Kotlin

You can convert the date to unix timestamp using following.

val date = SimpleDateFormat("dd-MM-yyyy").parse("04-07-2021") println(date.time)


More about date time in Kotlin