How to extract date and time from Long type variable in Kotlin on Android

"properties": {
    "value": 3.2,
    "address": "Kairo",
    "time": 1595386325905,
    "updated": 1415323859614,
}

 

With Below code we can get date and time

import java.text.SimpleDateFormat
import java.util.Date

fun convertLongToTime (time: Long): String {
    val date = Date(time)
    val format = SimpleDateFormat("dd/M/yyyy hh:mm:ss")
    return format.format(date)
}

 

 

From API level 26 on (Android) or if JVM is your target, we can use Java 8 Date API:

val date = Instant
        .ofEpochMilli(1164925597950)
        .atZone(ZoneId.systemDefault()) // change time zone if necessary
        .toLocalDateTime()

val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
println(formatter.format(date))

 

Below API level 26:

 

 

val date = Date(1595386325905)

val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
println(formatter.format(date))

 


Subscribe For Daily Updates

Flutter Questions
Android Questions