0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kenny Dabiri
  • 353
  • 3
  • 22

1 Answers1

0

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));
}
Wilik
  • 7,630
  • 3
  • 29
  • 35