Iterate between two dates including start date?
Solution 1
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).
Solution 2
You could use a do-while
loop, but you would need to alter the end
date depending on whether you want to include it or not.
The example below includes all months between 01st or 04th inclusive...
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 start
Date
to one month earlier...
Date start = sdf.parse("2009.12");
//...
Updated
Another approach, based on the previous ideas...
Simply move the start
date back a month before you start the loop...
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....
Solution 3
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);
}
Solution 4
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);
}
Solution 5
Since Java 8, you can use the new java.time
API:
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);
}
SWEE
Updated on June 04, 2022Comments
-
SWEE almost 2 years
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..
-
Thihara almost 10 yearsWhy not move gcal.add(Calendar.MONTH, 1); to the end of the while body? Simpler no?
-
Thihara almost 10 yearsI mean without moving to the do while loop.
-
MadProgrammer almost 10 years@Thihara Because then it won't print the last month either. It would actually be simpler to modify either
start
orend
date values based on which technique the OP choose to use ;) -
MadProgrammer almost 10 yearsI'd run with that +1 for getting both the start and end dates included
-
Thihara almost 10 yearsDamn, outsmarted by a mad dude :-P, +1
-
MadProgrammer almost 10 years@Thihara There's always more the one way to skin that cat ;) - These are just a few suggestions ;)
-
George almost 10 yearsWrestling with Java Calendars is one of my specialties.