如果我在数据库中存储了ServerValue.TIMESTAMP的时间,如何从FirebaseFirestore中检索RecyclerView中的日期和时间

时间:2018-02-20 08:31:03

标签: java android firebase android-recyclerview google-cloud-firestore

我正在创建一个应用程序,用户可以在其中发布信息,在发布时我通过在Firestore数据库中使用order=2来保存帖子的时间。

问题是时间会像地图一样保存在数据库中

ServerValue.TIMESTAMP

以下是Firestore in the rightmost column you can see how my time is getting saved in the database

中我的数据库的图片

现在,我想提取人类可读的日期和时间,以time: .sv : "timestamp" 显示给用户。

这是我的模特课程,我希望通过该课程获得时间,但尚未包含任何数据成员,因为我不知道如何完成它。

RecyclerView

以下是我的适配器类

public class PostDisplayModel extends TransferId{

public String teacherName;
public String topic;

PostDisplayModel(){}

public PostDisplayModel(String teacherName, String topic) {
    this.teacherName = teacherName;
    this.topic = topic;
}

public String getTeacherName() {
    return teacherName;
}

public void setTeacherName(String teacherName) {
    this.teacherName = teacherName;
}

public String getTopic() {
    return topic;
}

public void setTopic(String topic) {
    this.topic = topic;
}
}

我想在textview中使用正确的时间戳格式,如 yyyy-MM-dd hh:mm:ss ,我正在从回收站视图中正确获取所有其他数据。

任何建议/帮助将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您将时间戳设置为ServerValue.TIMESTAMP。使用Firebase实时数据库时,这是正确的方法。如果是Cloud Firestore,则需要将其设置为 FieldValue.serverTimestamp()

This 是您使用模型类在Cloud Firestore数据库中设置TIMESTAMP的正确方法。