java - 如何在 Spring Boot 应用程序中实现 Firestore 时间戳
问题描述
我正在创建一个 Spring Boot 应用程序,我在 Firestore DB 中有一个时间戳变量,我想在该字段中发送日期。我怎样才能做到这一点?
这是我向 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;
}
解决方案
正如上面@Zeenath SN 所提到的,这篇文章有你的答案。
您可以使用ServerTimestamp注释。尝试改变
private Timestamp dob;
至
private Date dob;
在你的代码中。
或者,如下所示:
如果要编写时间戳字段,并且不能使用服务器时间戳,则需要提供常规 Java Date 对象或 Firebase Timestamp 对象。这些类型的值中的任何一种都将转换为文档中的时间戳字段。
当您查询文档时,该字段将始终作为时间戳类型对象显示在文档快照中。
推荐阅读
- java - QuartzJobBean 不执行春季启动
- django - 如何在html中显示每个评分周期的平均值?使用 django
- docker - 使用自定义图像禁用 docker md5sum
- c++ - boost::property_tree::xml_parser::read_xml 是否保留顺序?
- android - 使用 Flutter 进行 Firebase 电话身份验证
- reactjs - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 from WordPress
- javascript - 如何在 React Native 中将输入字段添加到列表中?
- plsql - 使用用户定义类型努力收集和返回 DBMS_SQL.COLUMN_VALUE
- python - 根据通过 BeautifulSoup 找到的元素在 Pandas 中插入新列
- selenium - WinAppDriver 有时找不到元素