Applying Date Formatting

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 6.  Formatting Data with Tags


Now that you know how to format dates and times in various ways, let's see how to apply these tags. Listing 6.7 shows a program that displays a calendar.

Listing 6.7 Displaying a Calendar (calendar.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%@  taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <html>   <head>     <title>Calendar</title>   </head>   <body>     <form method="POST">       <table border="1" cellpadding="0" cellspacing="0"       style="border-collapse: collapse" bordercolor="#111111"       width="62%" >         <tr>           <td width="100%" colspan="2" bgcolor="#0000FF">             <p align="center">               <b>                 <font color="#FFFFFF" size="4">Date                 Formatting</font>               </b>             </p>           </td>         </tr>         <tr>           <td width="47%">Enter a month(1-12)</td>           <td width="53%">             <input type="text" name="month" size="20" />           </td>         </tr>         <tr>           <td width="47%">Enter a year(i.e. 2002)</td>           <td width="53%">             <input type="text" name="year" size="20" />           </td>         </tr>         <tr>           <td width="100%" colspan="2">             <p align="center">               <input type="submit" value="Submit" name="submit" />               <input type="reset" value="Reset" name="reset" />             </p>           </td>         </tr>       </table>       <p>&#160;</p>     </form>     <c:if test="${pageContext.request.method=='POST'}">       <table border="1" cellpadding="0" cellspacing="0"       style="border-collapse: collapse" bordercolor="#111111"       width="63%" >         <fmt:parseDate var="now"         value="${param.month}/1/${param.year}" type="date"         dateStyle="short" />         <tr>           <td width="100%" colspan="7" bgcolor="#0000FF">             <p align="center">               <b>                 <font color="#FFFFFF" size="4">                   <fmt:formatDate pattern="MMMMM yyyy"                   value="${now}" />                 </font>               </b>             </p>           </td>         </tr>         <fmt:formatDate var="i" pattern="E" value="${now}" />         <c:choose>           <c:when test="${i=='Sun'}">             <c:set var="i" value="1" />           </c:when>           <c:when test="${i=='Mon'}">             <c:set var="i" value="2" />           </c:when>           <c:when test="${i=='Tue'}">             <c:set var="i" value="3" />           </c:when>           <c:when test="${i=='Wed'}">             <c:set var="i" value="4" />           </c:when>           <c:when test="${i=='Thu'}">             <c:set var="i" value="5" />           </c:when>           <c:when test="${i=='Fri'}">             <c:set var="i" value="6" />           </c:when>           <c:when test="${i=='Sat'}">             <c:set var="i" value="7" />           </c:when>           <c:otherwise>             <c:set var="i" value="?" />           </c:otherwise>         </c:choose>         <c:choose>           <c:when test="${param.month==2}">             <c:set var="max" value="28" />             <c:if             test="${ ((param.year % 4 == 0 && paran.year % 100 != 0)   || param.year % 400 == 0) }">               <c:set var="max" value="29" />             </c:if>           </c:when>           <c:when test="${param.month==4}">             <c:set var="max" value="30" />           </c:when>           <c:when test="${param.month==6}">             <c:set var="max" value="30" />           </c:when>           <c:when test="${param.month==9}">             <c:set var="max" value="30" />           </c:when>           <c:when test="${param.month==11}">             <c:set var="max" value="30" />           </c:when>           <c:otherwise>             <c:set var="max" value="31" />           </c:otherwise>         </c:choose>         <tr>           <td width="70">             <b>               <center>Sunday</center>             </b>           </td>           <td width="70">             <b>               <center>Monday</center>             </b>           </td>           <td width="70">             <b>               <center>Tuesday</center>             </b>           </td>           <td width="70">             <b>               <center>Wednesday</center>             </b>           </td>           <td width="70">             <b>               <center>Thursday</center>             </b>           </td>           <td width="70">             <b>               <center>Friday</center>             </b>           </td>           <td width="70">             <b>               <center>Saturday</center>             </b>           </td>         </tr>         <c:set var="d" value="1" />         <c:set var="d" value="1" />         <c:forEach var="x" begin="1" end="35">           <c:if           test="${(x==1)||(x==8)||(x==15)||(x==22)||(x==29)}">             </tr><tr>           </c:if>           <td>           <c:if           test="${ (d<=max) && ((x>7)||(i<=x)) }">             <c:out value="${d}" />             <c:set var="d" value="${d+1}" />           </c:if>           &#160;           <br />           <br />           <br />           </td>         </c:forEach>       </table>     </c:if>   </body> </html> 

The program in Listing 6.7 displays a calendar page for any specified month and year. To do this, the program must first determine what day of the week (for example, Monday) the first day of the specified month falls on. The following code accomplishes this:

<fmt:parseDate var="now" value="${param.month}/1/${param.year}" type="date" dateStyle="short" /> 

This code constructs a new date that is the "first day" of the specified year and month. We can then request the day of the week from the new date, as this line demonstrates:

<fmt:formatDate var="i" pattern="E" value="${now}" /> 

By requesting the formatting character "E", we are given the day of the week (Mon, Tue, and so forth). We must now convert the text of the day of the week to a "column number." This tells us which column to start numbering on when the calendar is displayed. The following code does this conversion for the first two weekdays:

<c:when test="${i=='Sun'}">   <c:set var="i" value="1" /> </c:when> <c:when test="${i=='Mon'}">   <c:set var="i" value="2" /> </c:when> 

Now that we know what column number to start on, we know where to begin. Next, we must decide where to end, so we have to determine how many days are in the requested month. For most months, this is easy; there is a constant number of days for each month. The problem lies in February, which changes its length from 28 to 29 when the current year is a leap year. To handle February, we must determine whether this is a leap year.

This algorithm is relatively simple. A year is a leap year if it is divisible by 4. But if the year is also divisible by 100, then it is not a leap year unless it is divisible by 400. Years such as 1992 and 1996 are leap years because they are divisible by 4 and are not affected by the rest of the rule. The remaining part of the rule applies to century years, such as 1900 and 2000. Century years are not leap years except when they are a multiple of 400. Because of this, the years 1700, 1800, and 1900 were not leap years. But the infamous year 2000 was a leap year. Translated to a JSTL expression, this becomes:

<c:choose>   <c:when test="${param.month==2}">     <c:set var="max" value="28" />     <c:if     test="${ ((param.year % 4 == 0 && paran.year % 100 != 0) ||   param.year % 400 == 0) }">       <c:set var="max" value="29" />     </c:if>   </c:when>   <c:when test="${param.month==4}">     <c:set var="max" value="30" />   </c:when> 

As you can see, an ordinary month such as April, month number four, is simply assigned 30 days. However, for February, month two, the leap year rule is applied. We have now determined the beginning and ending spots on the calendar grid for the requested calendar. The calendar is then drawn to an HTML table using a <c:forEach> tag. The output from the calendar program is shown in Figure 6.7.

Figure 6.7. Displaying a calendar.

graphics/06fig07.jpg


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch06lev1sec4]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net