如何在Android上从Kotlin中的长类型变量中提取日期和时间
我正在使用美国地质勘探局网站上的API如何在Android上从Kotlin中的长类型变量中提取日期和时间,android,kotlin,Android,Kotlin,我正在使用美国地质勘探局网站上的API "properties": { "mag": 3.2, "place": "Puerto Rico region", "time": 1164925597950, "updated": 1415323859614, "tz": null } 时间以毫秒为单位。我想从这些毫秒中提取日期和时间。在Kotlin中如何做到这一点?以下是我在Java中的使用方法。您只需将其更改为kotlin即可。 SimpleDataFor
"properties": {
"mag": 3.2,
"place": "Puerto Rico region",
"time": 1164925597950,
"updated": 1415323859614,
"tz": null
}
时间以毫秒为单位。我想从这些毫秒中提取日期和时间。在Kotlin中如何做到这一点?以下是我在Java中的使用方法。您只需将其更改为kotlin即可。 SimpleDataFormat格式化程序=新的SimpleDataFormat(“dd/MM/yyyy”) String dateString=formatter.format(新日期(dateInMillis))
这将在Kotlin中工作,您可以根据需要在SimpleDataFormat中更改格式。只需将毫秒传递给日期构造函数
Date(timeinMillis)
它将返回日期 从Android上的API级别26开始,或者如果JVM是您的目标,您可以使用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)) // 2006-11-30 23:26
低于API等级26:
val date = Date(1164925597950)
val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
println(formatter.format(date)) // 2006-11-30 23:26
val date = Date(1164925597950)
val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm")
println(formatter.format(date)) // 2006-11-30 23:26