获取两个日期之间发生的日期列表

时间:2014-11-10 10:52:40

标签: java date

我正在尝试获取日期,这些日期从开始日期到结束日期每隔5天发生一次。

Eg.
if start date= 11/10/2014   i.e  MM/DD/YYYY format
and  end date =11/26/2014

then my **expected output** is =
 [11/15/2014,11/20/2014,11/25/2014]

我在下面试过但很混淆在哪里运行循环以获得准确的输出。目前,从下面的代码我只在列表中获得1个日期

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date; 
import java.util.List; 


public class TestDate {
        //     mm/dd/yyyy
    public List getDates(Date fromDate,int frequency,Date endDate){
        List list=new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


        Calendar c = Calendar.getInstance();
        c.setTime(fromDate); // Now use today date. 
        c.add(Calendar.DATE, frequency); // Adding 5 days  
        String newDate = sdf.format(c.getTime());
         String sEndDate=sdf.format(endDate);

        if((newDate.compareTo(sEndDate) < 0) || (newDate.compareTo(sEndDate) == 0)){
            list.add(newDate); 
        }

        //Weekly=7,Bi-Weekly14,Monthly-30,Semi-Monthly-15 
        return list; 
    }
    public static void main(String[] args) {
        try {
        TestDate obj=new TestDate();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date s = sdf.parse("11/10/2014");
        Date e = sdf.parse("11/26/2014");
        System.out.println(obj.getDates(s, 5, e));
        }
        catch(Exception e) { 
            System.err.println("--exp in main---"+e);   
        }
    }
}

正确答案低于 *Thanks to Almas*

public List getDates(Date fromDate,int frequency,Date e){
        List list=new ArrayList<Date>(); 
        Calendar c = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c2.setTime(e); // Now use today date.  
         Date endDate=c2.getTime();   
        Date newDate=fromDate;
        while(true){
            c.add(Calendar.DATE, frequency);
            newDate=c.getTime();

            if(newDate.compareTo(endDate)<=0){
                list.add(newDate);
            }else{
                break;
            }
        } 
        //Weekly=7,Bi-Weekly14,Monthly-30,Semi-Monthly-15 
        return list; 
    }

4 个答案:

答案 0 :(得分:1)

为什么您希望将日期作为字符串并按字典顺序进行比较,而不是将它们作为日期进行比较?

String newDate = sdf.format(c.getTime());
String sEndDate=sdf.format(endDate);

这应该改为

 Date newDate = c.getTime();

你也使用两个if条件,你可以在下面这样做:

if (newDate.compareTo(endDate) <= 0) {
    list.add(newDate);
}

就循环而言,您应该在getDates方法中执行此操作,如下所示:

Date newDate;
while (true) {
    c.add(Calendar.DATE, frequency); // Adding 5 days
    newDate = c.getTime();

    if (newDate.compareTo(endDate) <= 0) {
        list.add(newDate);
    } else {
        break;
    }
}    

答案 1 :(得分:0)

利用Apache commons DateUtils。这将使您的代码变得简单

    Date tempDate = DateUtils.addDays(fromDate, frequency);
    while (tempDate.before(endDate))
    {
        list.add(tempDate);
        tempDate = DateUtils.addDays(tempDate, frequency);
    }
    return list; 

答案 2 :(得分:0)

尝试这个

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class NewClass1 {
    //     mm/dd/yyyy

    public List getDates(Date fromDate, int frequency, Date endDate) {
        List list = new ArrayList<Date>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


        Calendar c = Calendar.getInstance();
        c.setTime(fromDate); // Now use today date.
        c.add(Calendar.DATE, frequency); // Adding 5 days
        String newDate = sdf.format(c.getTime());
        //System.out.println("date"+newDate);
        String formDate = sdf.format(fromDate);
        String sEndDate = sdf.format(endDate);
        int x = 1;
        while (((newDate.compareTo(sEndDate) > 0) || (newDate.compareTo(sEndDate) != 0)) && x < frequency) {
            c.add(Calendar.DATE, frequency);
            sEndDate = sdf.format(c.getTime());
            x++;
            System.out.println("date: " + sEndDate);
            list.add(newDate);
        }

        //Weekly=7,Bi-Weekly14,Monthly-30,Semi-Monthly-15
        return list;
    }

    public static void main(String[] args) {
        try {
            NewClass1 obj = new NewClass1();
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            Date s = sdf.parse("11/10/2014");
            Date e = sdf.parse("11/26/2014");
            obj.getDates(s, 5, e);
        } catch (Exception e) {
            System.err.println("--exp in main---" + e);
        }
    }
}

答案 3 :(得分:-2)

在您的代码中,您只需拨打

 list.add(...);

一次。

你需要一个(while / for)循环来获得预期的结果。