问题
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