2017-04-25 93 views
1

你好,我正在創建一個Android應用程序,列出校園周圍發生的本地事件。每個「事件」都有兒童用於存儲標題,圖像,類別,信息和日期。我想知道什麼是刪除當前日期事件的最好方法。日期格式爲mm/dd/yy。我在Java中使用日期函數的經驗並不多,可以使用一些建議。如何刪除「已過期」或超過Android Studio當前日期的Firebase項目?

這裏是example of an event。這裏是source code供參考。任何幫助是極大的讚賞。

+0

你爲什麼要刪除他們?如果你想過濾你的數據最好創建一個查詢。 – cutiko

回答

1

最好的做法是將您的數據保存爲TIMESTAMP像這樣ServerValue.TIMESTAMP

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

而要取回數據,我建議你使用這個方法:

public static String getTimeDate(long timeStamp){ 
    try{ 
     DateFormat dateFormat = getDateTimeInstance(); 
     Date netDate = (new Date(timeStamp)); 
     return dateFormat.format(netDate); 
    } catch(Exception e) { 
     return "date"; 
    } 
} 

解決你的問題,你只需要從數據庫中獲取的日期,並將其與當前日期比較和時間。如果TIMESTAMP的值小於當前日期和時間,則可以刪除該特定事件。

希望它有幫助。

0

您可以篩選日期本身火基準響應

例如:

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); 
}); 
相關問題