Skip to content Skip to sidebar Skip to footer

Populate Date And Time Into Textview From Firebase

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

Solution 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 = newHashMap<>();
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

publicstaticStringepochToFormatted(Long epoch) {
    SimpleDateFormat sdf = newSimpleDateFormat("dd MM yyyy");
    return sdf.format(newDate(epoch));
}

Post a Comment for "Populate Date And Time Into Textview From Firebase"