Skip to content Skip to sidebar Skip to footer

How To Delete Firebase Items That Are "expired" Or Past The Current Date On Android Studio?

Hello I am creating an Android app that lists local events happening around my campus. Each 'event' has children for storing the title, image, category, info, and date. I was wonde

Solution 1:

The best practice is to save your data as a TIMESTAMP like this ServerValue.TIMESTAMP.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map<String, Object> map = new HashMap<>();
map.put("time", ServerValue.TIMESTAMP);
ref.child("yourNode").updateChildren(map);

And to get the data back, i suggest you use this method:

publicstaticStringgetTimeDate(long timeStamp){
    try{
        DateFormat dateFormat = getDateTimeInstance();
        Date netDate = (newDate(timeStamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return"date";
    }
}

To solve your problem, you only need get the date from your database and compare it with the current date and time. If the value of TIMESTAMP is less than the current date and time, than you can delete that particular event.

Hope it helps.

Solution 2:

You can filter the fire base response on date itself

ex:

DatabaseReference mDatabaseReference =FirebaseDatabase.getInstance().getReference().child("table name");
ref.orderByChild("date").startAt(startDate).endAt(endDate).on("child_added", function(snapshot){
console.log("got the data!", snapshot);
});

Post a Comment for "How To Delete Firebase Items That Are "expired" Or Past The Current Date On Android Studio?"