gpt4 book ai didi

java - @ServerTimestamp 在 Firebase Firestore 上始终为 null

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:31 25 4
gpt4 key购买 nike

这是我的模型类:

import com.google.firebase.firestore.ServerTimestamp;
import java.util.Date;

public class YourModelClass {
private @ServerTimestamp Date timestamp;

YourModelClass() {}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

@Override
public String toString() {
return "YourModelClass{" +
"timestamp=" + timestamp +
'}';
}
}

当我在日志中打印时,我得到了 null

    Logger.info("Date", new YourModelClass()+"------"+new YourModelClass().getTimestamp() + "");

这是错误:

Output: 02-22 18:33:12.066 18980-18980/com.spendeye I/Date: YourModelClass{timestamp=null}------null

请让我知道解决方案。

最佳答案

这不是从数据库检索日期的正确方法。假设您使用 get() 调用,当检索 date 作为 Date 类型的对象时,您需要使用以下代码:

Date date = yourModelClass.getDate();
DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US); //Or any other locale
String creationDate = dateFormat.format(date);
Log.d("TAG", creationDate);

如您所见,我正在使用从数据库获取的格式化日期,我没有创建类的新实例。您也可以在我的 tutorials 之一中看到这种方法。 其中我已逐步解释如何添加和显示日期。

关于java - @ServerTimestamp 在 Firebase Firestore 上始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48928362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com