简体   繁体   中英

find if a value of Date exists in arraylist of Calendar

i want to verify that a Calendar Date not in a Arraylist of Calendar in java, I do this but it's not working for me :

List<Calendar> listFeriers = new ArrayList<Calendar>();
startCal = Calendar.getInstance();
startCal.setTime(beginningDate);
endCal = Calendar.getInstance();
endCal.setTime(endDate);
do {
    if (!listFeriers.contains(startCal)) {
        // Traitement
    }
} while (startCal.before(endCal));
List<Calendar> listFeriers = new ArrayList<Calendar>();

I hope you populate listFeriers somewhere.

You could just store the time in millis (getTimeInMillis) in the list rather than creating too many Calendar objects. You can use set to avoid duplicate time entries.

Set<Long> listFeriers = new HashSet<Long>();

if(listFeriers.contains(startCal.getTimeInMillis())){
    // A match..
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM