There are a list of dates in my program and whenever we start the app (or whenever the system date changes), the app should check and remove the dates which are in the past.
I've tried the following code but it doesn't really work
delDateRef = FirebaseDatabase.getInstance().getReference();
Date today1 = new Date();
Date tomorrow = new Date(today1.getTime() + (1000 * 60 * 60 * 24));
for (int i=0; i<dates.size(); i++) {
if (dates.get(i).before(tomorrow)) {
Toast.makeText(RoomDetails.this, "Here", Toast.LENGTH_SHORT).show();
delDateRef = delDateRef.child("venues").child(roomNo).child("bookings").child(parentNodes.get(i));
delDateRef.removeValue();
singleBookingItemList.remove(i);
parentNodes.remove(i);
i--;
roomBookingsAdapter.notifyDataSetChanged();
}
}
If today's date is X, it should stay. But once today's date changes and the current day is X+1, the date X should be removed from the list of dates.
Try this:
final Handler someHandler = new Handler(getMainLooper());
someHandler.postDelayed(new Runnable() {
@Override
public void run() {
tvClock.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));
someHandler.postDelayed(this, 1000);
}
}, 10);
To remove all past/old dates from a list of dates use the method
private ArrayList<Date> getActiveDates(ArrayList<Date> all_dates) {
//sort array for faster loop
Arrays.sort(all_dates.toArray());
Date today = new Date();
ArrayList<Date> today_and_future = all_dates;
for(Date curDate : all_dates){
if (curDate.compareTo(today) >= 0){
//we have gotten past all old dates
//break from loop
break;
}
//remove old date
today_and_future.remove(curDate);
}
return today_and_future;
}
Call from your code
delDateRef = FirebaseDatabase.getInstance().getReference();
for (Date activeDate : getActiveDates(dates)) {
...
//make use of activeDate object here
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.