我正在使用Firestore,当我从Firestore获得时间戳时会卡住。我如何将此Timestamp(seconds=1556006384, nanoseconds=994000000)
转换为Kotlin中的Date。
我的最低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)
谢谢。
答案 0 :(得分:2)
将两个答案都放在首位,这对我有用。
val timestamp = data[TIMESTAMP] as com.google.firebase.Timestamp
val date = timestamp.toDate()
答案 1 :(得分:0)
答案 2 :(得分:0)
好的,我正在使用这些代码
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)
答案 3 :(得分:0)
我使用此类来序列化时间戳
import com.google.firebase.Timestamp
data class Article (
var createdAt: Timestamp? = Timestamp.now(),
var title: String = "",
var image: String = ""
)