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