首页 > 解决方案 > 如何在 Spring Boot 应用程序中实现 Firestore 时间戳

问题描述

我正在创建一个 Spring Boot 应用程序,我在 Firestore DB 中有一个时间戳变量,我想在该字段中发送日期。我怎样才能做到这一点?

这是来自我的 Firebase DB 我使用 dob 作为时间戳

这是我用来从数据库调用该时间戳的数据类型,但这是错误的,我无法发送正确的日期

这是我向 Firebase 发送数据的服务

    public Patient addPatient(Patient patients) throws InterruptedException, ExecutionException  {
    
    Firestore dbFirestore = FirestoreClient.getFirestore();
    ApiFuture<WriteResult> collectionApiResult = dbFirestore.collection(this.fireStoreCollection).document(patients.getUserId()).set(patients);
    return patients;
}

我的 Firestore DB 屏幕截图我想在此发送 dob

标签: javaspring-bootapigoogle-cloud-firestore

解决方案


正如上面@Zeenath SN 所提到的,这篇文章有你的答案。

您可以使用ServerTimestamp注释。尝试改变

private Timestamp dob;

private Date dob;

在你的代码中。

或者,如下所示:

如果要编写时间戳字段,并且不能使用服务器时间戳,则需要提供常规 Java Date 对象或 Firebase Timestamp 对象。这些类型的值中的任何一种都将转换为文档中的时间戳字段。

当您查询文档时,该字段将始终作为时间戳类型对象显示在文档快照中。


推荐阅读