[英]How do I convert a Firestore date/Timestamp to Date in Kotlin?
i'm working with firestore, and getting stuck when i get the timestamp from firestore.我正在使用 firestore,当我从 firestore 获取时间戳时卡住了。 how i convert this
Timestamp(seconds=1556006384, nanoseconds=994000000)
to Date in kotlin.我如何将此
Timestamp(seconds=1556006384, nanoseconds=994000000)
转换为科特林中的日期。
my minimum SDK is 21 so the code below doesn't work我的最小 SDK 是 21,所以下面的代码不起作用
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)
thank you.谢谢你。
I'm using this code:我正在使用这段代码:
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)
Combined both answers on top and this is what worked for me.结合上面的两个答案,这对我有用。
val timestamp = data[TIMESTAMP] as com.google.firebase.Timestamp
val date = timestamp.toDate()
I used this class to serialize the Timestamp
我用这个类来序列化时间戳
import com.google.firebase.Timestamp
data class Article (
var createdAt: Timestamp? = Timestamp.now(),
var title: String = "",
var image: String = ""
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.