在两个日期之间进行迭代,包括开始日期?

SWE*_*WEE 3 java calendar gregorian-calendar simpledateformat

对不起要求重复提问的道歉..

public static void main(String[] args)throws Exception {
    GregorianCalendar gcal = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
    Date start = sdf.parse("2010.01");
    Date end = sdf.parse("2010.04");
    gcal.setTime(start);
    while (gcal.getTime().before(end)) {
        gcal.add(Calendar.MONTH, 1);
        Date d = gcal.getTime();
        System.out.println(d);
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码打印日期之间,但我需要打印开始日期也..

上面的代码输出是

Mon Feb 01 00:00:00 IST 2010
Mon Mar 01 00:00:00 IST 2010
Thu Apr 01 00:00:00 IST 2010
Run Code Online (Sandbox Code Playgroud)

但我还需要在输出上开始约会..

请帮我提前谢谢..

Geo*_*rge 8

在我看来,这是最好的方式:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");

GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(start);

while (!gcal.getTime().after(end)) {
    Date d = gcal.getTime();
    System.out.println(d);
    gcal.add(Calendar.MONTH, 1);
}
Run Code Online (Sandbox Code Playgroud)

输出:

Fri Jan 01 00:00:00 WST 2010
Mon Feb 01 00:00:00 WST 2010
Mon Mar 01 00:00:00 WST 2010
Thu Apr 01 00:00:00 WST 2010
Run Code Online (Sandbox Code Playgroud)

我们所做的只是在递增之前打印日期,然后如果日期不在结束日期之后我们重复.

另一种选择是在while(yuck)之前复制打印代码或使用do...while(也是yuck).