Accessibility
 
Home > Products > UltraDev > Support > Building Common Applications
Dreamweaver UltraDev Icon Macromedia Dreamweaver UltraDev Support Center - Building Common Applications
Creating a list of consecutive dates

Your design calls for displaying a list of fourteen consecutive dates starting with the current date. The basic logic to achieve this effect is simple. First, create a loop that repeats a block of code fourteen times. Each time the block is executed, assign the correct date information to variables, use the variables to write the date to the page, and increment the date by one day. Here's the logic expressed in pseudo-code:

repeat following block 14 times
	assign the date information to variables
	write the date using the variables
	increment the date by one day

Here's the same logic expressed in a Java scriptlet:

<%
GregorianCalendar dateInfo = new GregorianCalendar();

for (int i = 0; i < 14; i++) {
	String day = dayNames[dateInfo.get(dateInfo.DAY_OF_WEEK) - 1];
	String month = monthNames[dateInfo.get(dateInfo.MONTH)];
	int date = dateInfo.get(dateInfo.DATE);

	out.print(day + ", " + month + " " + date + "<br>");

	dateInfo.roll(dateInfo.DAY_OF_YEAR, true);
}
%>

The for statement starts a loop that repeats the block of code between the curly brackets fourteen times. The three statements following the first curly bracket assign date information to variables. The out.print() method uses the variables to write the date in the HTML code. The last statement uses the roll() method of the GregorianCalendar class to increment the date by one day. The first argument of the roll() method specifies what object field to modify (here, dateInfo.DATE_OF_YEAR); the second argument specifies whether to increment (true) or decrement (false) the field by 1.

When you run the page, the following list is displayed:

Your task is done.

To Table of Contents Back to Previous document