简体   繁体   English

如何将 Firestore 日期/时间戳转换为 Kotlin 中的日期?

[英]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.谢谢你。

Call toDate() on a Firestore Timestamp object to return a Date object.在 Firestore Timestamp对象上调用toDate()以返回 Date 对象。

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.

相关问题 如何将 Firestore 日期/时间戳转换为 JS Date()? - How do I convert a Firestore date/Timestamp to a JS Date()? 如何将 Firestore 时间戳转换为 Date() - Firestore V9 - How to convert Firestore Timestamp to Date() - Firestore V9 Kotlin:将当前日期存储在 Firestore 中作为时间戳 - Kotlin: Store current date in Firestore as timestamp 将firestore时间戳转换为不同的格式 - convert firestore timestamp to date into different format 将字符串转换为日期,然后转换为时间戳以添加到 firestore - Convert String to Date then to timestamp to add to firestore 将日期转换为时间戳以存储到 javascript 中的 firebase firestore - Convert date to timestamp for storing into firebase firestore in javascript Pyspark - 如何在 pyspark 中转换格式为 /Date(1593786688000+0200)/ 的日期/时间戳? - Pyspark - How do I convert date/timestamp of format like /Date(1593786688000+0200)/ in pyspark? 如何序列化 json firestore Timestamp to Date? - How to serialize a json firestore Timestamp to Date? 如何将 Firestore 时间戳打印为格式化的日期和时间 - How to print Firestore timestamp as formatted date and time Vue Firebase - 将 Firestore 时间戳值数组转换为可读的 javascript 日期 - Vue Firebase - convert array of firestore timestamp value to readable javascript date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM