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

前端 未结 2 384
鱼传尺愫
鱼传尺愫 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 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.

提交回复
热议问题