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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24881756/
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-08-14 15:20:16  来源:igfitidea点击:

Iterate between two dates including start date?

javacalendarsimpledateformatgregorian-calendar

提问by SWEE

Sorry for the apology for asking repeated question..

很抱歉为重复提问而道歉..

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

In the above code prints between dates exactly but i need to print start date also..

在上面的代码中准确地打印日期之间,但我还需要打印开始日期..

above code output is

上面的代码输出是

Mon Feb 01 00:00:00 IST 2010
Mon Mar 01 00:00:00 IST 2010
Thu Apr 01 00:00:00 IST 2010

But i need also start date on my output..

但我还需要输出的开始日期..

please help me to get this Thanks in advance..

请帮我得到这个提前谢谢..

采纳答案by George

In my opinion this is the nicest way:

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

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

Output:

输出:

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

All we do is print the date before incrementing it, then we repeat if the date is not after the end date.

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

The other option is to duplicate the printing code before the while(yuck) or to use a do...while(also yuck).

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

回答by Hyman

You can just output it right before the start of the loop:

您可以在循环开始之前输出它:

System.out.println(gcal.getTime());
while (gcal.getTime().before(end)) {
    gcal.add(Calendar.MONTH, 1);
    Date d = gcal.getTime();
    System.out.println(d);
}

回答by Stefan Beike

then just do this:

然后就这样做:

Date lNow = gcal.getTime();
System.out.println(lNow);

while (gcal.getTime().before(end)) {
        gcal.add(Calendar.MONTH, 1);
        Date d = gcal.getTime();
        System.out.println(d);
}

回答by MadProgrammer

You could use a do-whileloop, but you would need to alter the enddate depending on whether you want to include it or not.

您可以使用do-while循环,但您需要end根据是否要包含它来更改日期。

The example below includes all months between 01st or 04th inclusive...

以下示例包括 01 日或 04 日之间的所有月份...

try {
    Calendar gcal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
    Date start = sdf.parse("2010.01");
    Date end = sdf.parse("2010.05");
    gcal.setTime(start);
    do {
        Date d = gcal.getTime();
        System.out.println(d);
        gcal.add(Calendar.MONTH, 1);
    } while (gcal.getTime().before(end));
} catch (ParseException exp) {
    exp.printStackTrace();
}

Example output...

示例输出...

Fri Jan 01 00:00:00 EST 2010
Mon Feb 01 00:00:00 EST 2010
Mon Mar 01 00:00:00 EST 2010
Thu Apr 01 00:00:00 EST 2010

Equally, you could simply use your current code and change the startDateto one month earlier...

同样,您可以简单地使用您当前的代码并将其更改startDate为一个月前...

Date start = sdf.parse("2009.12");
//...

Updated

更新

Another approach, based on the previous ideas...

另一种方法,基于以前的想法......

Simply move the startdate back a month before you start the loop...

只需start在开始循环之前将日期向后移动一个月......

Calendar gcal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
Date start = sdf.parse("2010.01");
Date end = sdf.parse("2010.04");
gcal.setTime(start);
// Move the month back by one before we start...
gcal.add(Calendar.MONTH, -1);
while (gcal.getTime().before(end)) {
    gcal.add(Calendar.MONTH, 1);
    Date d = gcal.getTime();
    System.out.println(d);
}

This is reasonably simple and allows you to supply variable dates without needing to care to remember that you need to start one month earlier....

这相当简单,允许您提供可变日期,而无需记住您需要提前一个月开始......

回答by isnot2bad

Since Java 8, you can use the new java.timeAPI:

从 Java 8 开始,您可以使用新的java.timeAPI:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu.MM");
YearMonth date = YearMonth.parse("2010.01", formatter);
YearMonth end = YearMonth.parse("2010.05", formatter);
while (!date.isAfter(end)) {
    System.out.println(date.format(formatter));
    date = date.plusMonths(1);
}