calendar + date question

Say I have, fromDate : "10.09.2001" and toDate : "29.02.2004". I want to:
1. display every month and year in between them:
    i.e. : 09, 2001
             10, 2001
             11, 2001
             12, 2001
             01, 2002
             02, 2002
             02, 2004.

2. display every day, month and year between them.
   i.e. : 10, 09, 2001
         11, 09, 2001
         12, 09, 2001
         31, 12, 2001
         01, 01, 2002
         02, 01, 2002
         28, 02, 2004
         29, 02, 2004.
LVL 32
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

create a calendar with the "fromDate", and for the month/year version, set the month to be get(MONTH)+1, keep looping till this calendar.getTime().getTime() > toDate.getTime().
For the days, do the same, but use calendar.set( calendar.get( DAY ) + 1 ) to do the increment...
this will work as the Calendar handles correctly incrementing days, months, years as you only "increment" the days (or months for the first example), so, e.g. if calendar.get( Calendar.DAY_OF_MONTH ) returns 31 (e.g. 31st December 2001) the result of "calendar.set( Calendar.DAY_OF_MONTH, calendar.get( Calendar.DAY_OF_MONTH )  + 1 )" will be 1st Jan, 2002...
i.e. sample code for the day-at-a-time version...

        GregorianCalendar incCal = new GregorianCalendar();
        incCal.setTime( fromDate );
        SimpleDateFormat formatter = new SimpleDateFormat( "dd, MM, yyyy" );
        while( incCal.getTime().getTime() < toDate.getTime() ) {
            System.out.println( formatter.format( incCal.getTime() ) );
            incCal.set( Calendar.DAY_OF_MONTH, incCal.get( Calendar.DAY_OF_MONTH ) + 1 );


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

calendar.set( Calendar.DAY_OF_MONTH, calendar.get( Calendar.DAY_OF_MONTH )  + 1 )
Instead of this, you should use calendar.add( Calendar.DAY_OF_MONTH, 1);

The other way, you may be setting 32 to a day. This is ok if the Calendar is set to lenient true (meaning that overflows to fields will roll the next field), but will throw exception if lenient = false (anyway, true is default).
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
displayDates(sdf.parse("10.09.2001" ), sdf.parse("28.02.2002"));


void displayDates(Date fromDate, Date toDate) {
      Calendar cal = Calendar.getInstance();
      cal.setTime( fromDate );
      SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
      while( cal.getTime().getTime() < toDate.getTime() ) {
            out.println( formatter.format( cal.getTime() ) );
            cal.add(Calendar.DATE, 1);
You might need that to be

while(cal.getTime().getTime() <= toDate.getTime()) {
A small alteration will allow you to switch between days and months:

displayDates(sdf.parse("10.09.2001" ), sdf.parse("28.02.2002"), Calendar.MONTH);
displayDates(sdf.parse("10.09.2001" ), sdf.parse("28.02.2002"), Calendar.DATE);

void displayDates(Date fromDate, Date toDate, int period)

      cal.add(period, 1);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.