如何在Firestore中保存时间戳? [英] How to save timestamp in Firestore?

查看:47
本文介绍了如何在Firestore中保存时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用聊天应用程序,并且想存储消息的时间戳.我的消息数据类是:

I am working on a chat app and I want to store timestamp of my messages. My Data Class for Messages is:

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属性?

I don't understand how to make object of this class. What can I pass in the constructor of this class to initialize timestamp attribute?

推荐答案

要解决此问题,您应删除 Date time 作为构造函数的参数.您的构造函数应如下所示:

To solve this, you should remove the Date time as the argument of your constructor. Your constructor should be like this:

public messgaesDataClass(String messageText, int messageStatus, String messagefrom) {
    this.messageText = messageText;
    this.messageStatus = messageStatus;
    this.messagefrom = messagefrom;
}

不需要在构造函数中初始化 time 对象.Firebase服务器将读取您的 time 字段,因为它是 ServerTimestamp (由于有注释),并且将相应地填充服务器时间戳.

There is no need to initialize the time object in your constructor. Firebase servers will read your time field as it is a ServerTimestamp (because of the annotation), and it will populate that filed with the server timestamp accordingly.

这篇关于如何在Firestore中保存时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆