如何在Kotlin中将Firestore日期/时间戳转换为Date? [英] How do I convert a Firestore date/Timestamp to Date in Kotlin?
本文介绍了如何在Kotlin中将Firestore日期/时间戳转换为Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Firestore,但是当我从Firestore获得时间戳时,它会卡住.如何将 Timestamp(seconds = 1556006384,nanoseconds = 994000000)
转换为Kotlin中的日期.
i'm working with firestore, and getting stuck when i get the timestamp from firestore. how i convert this Timestamp(seconds=1556006384, nanoseconds=994000000)
to Date in kotlin.
我的最低SDK为21,因此下面的代码不起作用
my minimum SDK is 21 so the code below doesn't work
val timestamp = it["time"] as com.google.firebase.Timestamp
val milliseconds = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000
val tz = ZoneId.of("Asia/Jakarta (UTC+07:00)")
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), tz)
谢谢.
推荐答案
我正在使用以下代码:
val timestamp = it["time"] as com.google.firebase.Timestamp
val milliseconds = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000
val sdf = SimpleDateFormat("MM/dd/yyyy")
val netDate = Date(milliseconds)
val date = sdf.format(netDate).toString()
Log.d("TAG170", date)
这篇关于如何在Kotlin中将Firestore日期/时间戳转换为Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文