java 从 List<Date> 创建一个 List<List<Date>> ,其中包含随后放入 Lists 的所有日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3592176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:33:50  来源:igfitidea点击:

Create from a List<Date> a List<List<Date>> that has all Dates which are subsequently put in Lists

javacalendar

提问by tzippy

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.

我希望你理解我。对我来说很难用英语描述。所以我有一个排序的日期对象列表。现在我想将所有连续的日期(12/16/09、12/17/09、12/18/09...)放在他们自己的列表中。独立日期也应放在它们自己的列表中。

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)我想创造

[(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...

我的代码有效。但它是如此复杂,我想知道是否有更简单的方法。不幸的是,我不能使用 Joda-Time。也许之前有人做过...

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;
 }  

回答by Mike Q

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);