Populate Date and Time into Textview from Firebase

ぃ、小莉子 提交于 2019-12-25 07:27:22

问题


I created a listview with two textviews, i want to get the date and time specific to any data added to the database into the textview from firebase.

Is there any method to achieve the aforementioned?

Thanks.


回答1:


You need to add another child to store the timestamp when saving the data.

Your data structure should look something like this

"data": {
    "id1": {
        "text": "some text",
        "timestamp": 1472861629000
    },
    "id2": {
        "text": "some text",
        "timestamp": 1472861629000
    },
    "..."
}

To save the timestamp, you can pass a firebase constant ServerValue.TIMESTAMP that will be converted to an epoch time based on firebase server time

Map<String, Object> values = new HashMap<>();
values.put("text", "some text");
values.put("timestamp", ServerValue.TIMESTAMP);
Database.ref().child("data").push().setValue(values);

To convert the epoch time to human readable date, use this method

public static String epochToFormatted(Long epoch) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy");
    return sdf.format(new Date(epoch));
}


来源:https://stackoverflow.com/questions/39300152/populate-date-and-time-into-textview-from-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!