How to delete firebase items that are “expired” or past the current date on Android Studio?

前端 未结 2 382
鱼传尺愫
鱼传尺愫 2021-01-23 20:58

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

相关标签:
2条回答
  • 2021-01-23 21:13

    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:

    public static String getTimeDate(long timeStamp){
        try{
            DateFormat dateFormat = getDateTimeInstance();
            Date netDate = (new Date(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.

    0 讨论(0)
  • 2021-01-23 21:20

    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);
    });
    
    0 讨论(0)
提交回复
热议问题