Java @Firebase Firestore上的ServerTimestamp始终为空

Java @Firebase Firestore上的ServerTimestamp始终为空,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,这是我的模型课: import com.google.firebase.firestore.ServerTimestamp; import java.util.Date; public class YourModelClass { private @ServerTimestamp Date timestamp; YourModelClass() {} public Date getTimestamp()

这是我的模型课:

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);

如您所见,我使用的是从数据库获得的格式化日期,而不是创建类的新实例。您也可以在我的一个中看到这种方法,我在其中逐步解释了如何添加和显示日期。

如果您在将日志存储到Firestore之前打印日志,然后,在Firestore上保存之前,任何类中具有
@ServerTimestamp
的属性
timestamp
的值始终为空

因此,您将得到null


如果您可以删除log语句并让对象保存在Firestore上,那么当您从Firestore取回该类对象时,log语句将是不同的,不会显示为null。

您可以发布用于将servertime上载到firebase的代码吗?而且,在firebase数据库中,名称与POJO相同?这很重要