我正在使用聊天应用程序,我想存储消息的时间戳。我的消息数据类是:
import com.google.firebase.firestore.ServerTimestamp;
import java.util.Date;
public class messgaesDataClass {
private String messageText;
private int messageStatus;
private String messagefrom;
private @ServerTimestamp Date timestamp;
public messgaesDataClass(String messageText, int messageStatus, String messagefrom, Date time) {
this.messageText = messageText;
this.messageStatus = messageStatus;
this.messagefrom = messagefrom;
this.time = time;
}
public messgaesDataClass() {
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public int getMessageStatus() {
return messageStatus;
}
public void setMessageStatus(int messageStatus) {
this.messageStatus = messageStatus;
}
public String getMessagefrom() {
return messagefrom;
}
public void setMessagefrom(String messagefrom) {
this.messagefrom = messagefrom;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
我不明白如何制作此类的对象。我可以在此类的构造函数中传递什么以初始化timestamp属性?
答案 0 :(得分:4)
要解决此问题,应删除Date time
作为构造函数的参数。您的构造函数应如下所示:
public messgaesDataClass(String messageText, int messageStatus, String messagefrom) {
this.messageText = messageText;
this.messageStatus = messageStatus;
this.messagefrom = messagefrom;
}
您无需在构造函数中初始化time
对象。 Firebase服务器将读取您的time
字段,因为它是一个ServerTimestamp
(由于有注释),并将相应地使用服务器时间戳填充该字段。