Displaying the current date
To display the current date using server-side scripting, you start by creating a GregorianCalendar object by inserting the following scriptlet in the page:
<%
GregorianCalendar dateInfo = new GregorianCalendar();
%>
This statement creates a GregorianCalendar object called dateInfo. By default, the statement assigns the server's current date and time to the object.
Next, you extract the date information from the object by using the get() method. The get() method returns values of type int (that is, a whole number), and takes one of the following arguments:
Argument |
Value Returned |
AM_PM |
0 or 1, corresponding to a.m. or p.m. |
DAY_OF_WEEK |
1 to 7, corresponding to the days Sunday to Saturday |
MONTH |
0 to 11, corresponding to the months January to December |
DATE |
1 to 31 |
DAY_OF_YEAR |
1 to 366 |
HOUR |
1 to 12, corresponding to the current hour in the a.m. or p.m. |
YEAR |
The current year |
For testing purposes, enter the following code in your page:
<%
GregorianCalendar dateInfo = new GregorianCalendar();
int weekDay = dateInfo.get(dateInfo.DAY_OF_WEEK);
int monthName = dateInfo.get(dateInfo.MONTH);
int date = dateInfo.get(dateInfo.DATE);
out.print(weekDay + ", " + monthName + " " + date);
%>
When you run the page, the following date is displayed:
6, 3 20
From the table above, you know 6 represents Friday (Sunday is day 1), and 3 represents April (January is month 0). Thus, the date on your server is Friday, April 20.
Expressing a date as 6, 3 20 is not very useful. Java has a class (java.text.DateFormatSymbols) that can convert the integers into day and month names, but it capitalizes the first letter of each name (Thursday instead of thursday). Your design calls for displaying day and month names all in lowercase, as follows:
You decide to solve the problem by creating your own custom names for days and months. The integer values returned by the get() method suggest a straightforward way of doing this: string arrays. First, you fill an array with names, then you use the integer value returned by the get() method to reference a specific element in the array"friday", for example.
You begin by creating and initializing two arrays, monthNames and weekDays, as follows:
<%
String[] monthNames = {"january","february","march","april","may","june",
"july","august","september","october","november","december"};
String[] dayNames = {"sunday","monday","tuesday","wednesday","thursday",
"friday","saturday"};
%>
The first element of both arrays start with an index of 0 (zero). For example, the expression monthNames[4] returns the array element "may". The expression dayNames[5] returns the array element "friday".
Next, you use the get() method to find the corresponding array elements and assign them to the variables day and month:
<%
GregorianCalendar dateInfo = new GregorianCalendar();
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>");
%>
Because the DAY_OF_WEEK expression returns a value of 1 to 7, you subtract 1 from it to match the 0-to-6 index of the dayNames array. The MONTH expression returns a value of 0 to 11, which matches the index of the monthNames array.
When you run the page, it displays the following date:
friday, april 20
The first half of the task is complete. Your design calls for the page to display fourteen dates starting with the current date. The next section describes how to create this list of consecutive dates.
|