tags:

views:

40

answers:

1

I hope you understood me. Hard to describe in english for me. So I have a List of sorted Date Objects. Now I want to put all Dates that are in a row (12/16/09, 12/17/09, 12/18/09...) in a List of their own. Stand-alone-Dates shall also be put in a List of their own.

So from (12/01/09; 12/02/09; 12/03/09; 12/06/09; 12/10/09; 12/17/09; 12/18/09; 12/31/09) I want to create

[(12/01/09; 12/02/09; 12/03/09)(12/06/09)(12/10/09)(12/17/09; 12/18/09)(12/31/09)]

The code I have works. But it is so complicated I was wondering if maybe there is an easier way. Unfortunately I can't use Joda-Time. maybe someone has done it before...

public List<List<Date>> listOfDatestoList(List<Date> dates){

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

    List<Date> theseDates = new ArrayList<Date>();


    if(dates.size() == 1){
        theseDates.add(dates.get(0));
        list.add(theseDates);
        return list;
    }

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

        //Die ersten beiden Daten der Liste werden verglichen
        cal1.setTime(dates.get(i));
        cal2.setTime(dates.get(i + 1));

        cal1.add(Calendar.DAY_OF_YEAR, 1);


        if(cal1.equals(cal2)){
            theseDates.add(dates.get(i));
        }

        else if(theseDates.size() > 0){
            theseDates.add(dates.get(i));

            List<Date> tempList = new ArrayList<Date>();
            for(Date date : theseDates){
                tempList.add(date); 
            }
            list.add(tempList);
            theseDates = new ArrayList<Date>();
        }
        else{
            List<Date> tempList = new ArrayList<Date>();

            tempList.add(dates.get(i));
            list.add(tempList); 
        }

        if(i == dates.size() - 2){

            if(cal1.equals(cal2)){
                theseDates.add(dates.get(i + 1));
            }

            if(theseDates.size() > 0 ){
                list.add(theseDates);
                theseDates = new ArrayList<Date>();
            }
            else{
                List<Date> tempList = new ArrayList<Date>();
                tempList.add(dates.get(i+1));
                list.add(tempList);
            }

        }
    }
    return list;
 }  
+2  A: 

An attempt, not tested but should be close to what you need (some date code pseudo-coded)

List<Date> dates = ...;
List<List<Date>> dateList = new ArrayList<List<Date>>();

List<Date> currentList = new ArrayList<Date>();
for (Date date : dates)
{
   if (currentList.isEmpty())
   {
       currentList.add(date);
       continue;
   }

   Date lastDate = currentList.get(currentList.size() - 1);

   if (date is one day after lastDate)
   {
       currentList.add(date);
       continue;
   }

   dateList.add(currentList);
   currentList = new ArrayList<Date>();
   currentList.add(date);
}

if (!currentList.isEmpty())
   dateList.add(currentList);
Mike Q
Not only close but damn good exaclty what my code does! much more elegant! thx!
tzippy