Solved

# calendar + date question

Posted on 2004-11-11
Medium Priority
243 Views
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.

AND

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.
0
Question by:ldbkutty
• 4
• 3

LVL 11

Expert Comment

ID: 12555442
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...
0

LVL 11

Expert Comment

ID: 12555475
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...
0

LVL 11

Accepted Solution

cjjclifford earned 900 total points
ID: 12555568
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 );
}

Cheers,
C.
0

LVL 3

Assisted Solution

Gunt earned 200 total points
ID: 12560970
calendar.set( Calendar.DAY_OF_MONTH, calendar.get( 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).
0

LVL 86

Assisted Solution

CEHJ earned 900 total points
ID: 12562192
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() ) );
}
}
0

LVL 86

Expert Comment

ID: 12562200
You might need that to be

while(cal.getTime().getTime() <= toDate.getTime()) {
0

LVL 86

Expert Comment

ID: 12562238
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)

...
...
}
0

LVL 86

Expert Comment

ID: 12774233
8-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
###### Suggested Courses
Course of the Month16 days, 10 hours left to enroll