开发者

Check if Dates from List<Date> are following each other

开发者 https://www.devze.com 2023-01-09 18:41 出处:网络
I 开发者_StackOverflowdon\'t know if I described it correctly. I have several lists of dates in Java. Now I need to know when a list contains at least 4 dates that follow each other. e.g. \"2010-06-27

I 开发者_StackOverflowdon't know if I described it correctly. I have several lists of dates in Java. Now I need to know when a list contains at least 4 dates that follow each other. e.g. "2010-06-27, 2010-06-28, 2010-06-29, 2010-06-30". I just need an idea where to start.... Thanks!


Iterate over the list and compare each date with the next one until you have four matching your criteria.


What about this:

public static boolean checkList(List<Date> list) {
    int count = 0;
    for (int i = 0; i < list.size()-1; ++i) {
        if (list.get(i).after(list.get(i+1))) {
            count = 0;
        } else {
            ++count;
            if (count == 4) {
                return true;
            }
        }
    }
    return false;
}


That's is my approach ...

    public List<List> getList(List<List<Date>> lists, int num) {
    List<List> result = new ArrayList<List>();
    for (List list : lists) {
        Collections.sort(list);
        if (checkList(list, num)) {
            result.add(list);
        }

    }
    return result;
}

public boolean checkList(List<Date> list, int num) {
    int count = 0;
    for (int i = 0; i < list.size() - 1; i++) {
        Calendar calendar1 = Calendar.getInstance();

        calendar1.setTime(list.get(i));
        calendar1.set(Calendar.DAY_OF_MONTH,calendar1.get(Calendar.DAY_OF_MONTH)+1);
        calendar1.set(Calendar.HOUR_OF_DAY,0);
        calendar1.set(Calendar.MINUTE,0);
        calendar1.set(Calendar.MILLISECOND,0);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(list.get(i + 1));
        calendar2.set(Calendar.HOUR_OF_DAY,0);
        calendar2.set(Calendar.MINUTE,0);
        calendar2.set(Calendar.MILLISECOND,0);

        if (calendar1.equals(calendar2)) {
            count++;
        } else {
            count = 0;
        }
        if (count >= num) {
            return true;
        }
    }
    return false;
}


Convert the date in miliseconds via date/calendar classes and then the comparation is trivial.


Now thats my approach. returns a list of Integers representing the length of each series of days in a row in that time period. Thanks Russel for your method. I tried compareto() of Date until i realized that its return value does not represent the days the two compared dates are seperated from each other.

     public List<Integer> countdaysinarow(List<Date> dates){
    int daysinarow = 0;  

    List<Integer> list = new ArrayList<Integer>();

    for (int i = 0; i < dates.size() - 1; i++) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(dates.get(i));
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(dates.get(i + 1));
        cal1.set(Calendar.DAY_OF_YEAR, cal1.get(Calendar.DAY_OF_YEAR) + 1);

        if(cal1.equals(cal2)){
            daysinarow++;
        }
        else if(daysinarow > 0){
                list.add(new Integer(daysinarow));
            daysinarow = 0;
        }

        if(daysinarow != 0 && (i == dates.size() - 2)){
            list.add(new Integer(daysinarow));
        }   
    }       
    return list;
 }  
0

精彩评论

暂无评论...
验证码 换一张
取 消