Kotlin:将当前日期存储在 Firestore 中作为时间戳

241*_*116 2 kotlin google-cloud-firestore

不知道为什么我找不到我要找的东西。但我只想将当前时间存储到 Firestore 文档中,如下所示:

在此输入图像描述

。在 Flutter 中我们只是这样做:

'timestamp': DateTime.now()
Run Code Online (Sandbox Code Playgroud)

我尝试在 kotlin 中这样做:

"timestamp" to LocalDateTime.now()
Run Code Online (Sandbox Code Playgroud)

但它给了我一些复杂的领域:

在此输入图像描述

Dou*_*son 6

所有平台上的最佳方法是使用 SDK 提供的服务器时间戳令牌(而不是使用客户端的时钟,这可能是错误的)。

文档在这里

// Update the timestamp field with the value from the server
val updates = hashMapOf<String, Any>(
        "timestamp" to FieldValue.serverTimestamp()
)

docRef.update(updates).addOnCompleteListener { }
Run Code Online (Sandbox Code Playgroud)

如果您确实想要客户端时钟的时间,只需传递 java Date withDate()Timestamp with Timestamp.now()