Chris Edgington
Chris Edgington

Reputation: 3236

How to parse Unix timestamp to date string in Kotlin

How can I parse a Unix timestamp to a date string in Kotlin?

For example 1532358895 to 2018-07-23T15:14:55Z

Upvotes: 19

Views: 25996

Answers (2)

jingx
jingx

Reputation: 4014

Or with the new Time API:

java.time.format.DateTimeFormatter.ISO_INSTANT
    .format(java.time.Instant.ofEpochSecond(1532358895))

Upvotes: 19

Erik Pragt
Erik Pragt

Reputation: 14637

The following should work. It's just using the Java libraries for handling this:

    val sdf = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
    val date = java.util.Date(1532358895 * 1000)
    sdf.format(date)

Upvotes: 28

Related Questions