Displaying all the months of the year
I have created a calendar program in jsp. But it only shows the current month 12 times,rather than displaying all the months. When the user enters the year, i want to display all the 12 months.
Here is the code for the calendar :
<%@page import="java.util.*,java.text.*" %>
<html>
<head>
<title>Print a month page.</title>
<meta name="version"
></head>
<body bgcolor="white">
<%
boolean yyok =false;
int yy = 0, mm = 0;
String yyString = request.getParameter("year");
if (yyString !=null && yyString.length( ) > 0)
{
try{
yy = Integer.parseInt(yyString);
yyok =true;
}
catch (NumberFormatException e)
{
out.println("Year " + yyString +" invalid");
}
}
Calendar c = Calendar.getInstance( );
if (!yyok)
yy = c.get(Calendar.YEAR);
mm = c.get(Calendar.MONTH);
for (int i=0; i<months.length; i++)
{
mm = i;
break;
}
%>
<form method=post action="CalendarPage.jsp">
Enter Year :
<input type="text" size="5" name="year" value=" <%= yy %>"></input>
<input type=submit value="Display">
</form>
<%!
String[] months =
{
"January","February","March","April",
"May","June","July","August",
"September","October","November","December"
};
int dom[] ={
31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31
};
%>
<%
int leadGap = 0;
%>
<table border=1>
<tr>
<th colspan=7>
<%= months[mm] %>
<%= yy %>
</tr>
<%
GregorianCalendar calendar =new GregorianCalendar(yy, mm, 1);
for(int j=1;j<=12;j++)
{
%>
<tr><td>Sunday<td>Monday<td>Tuesday<td>Wednesday<td>Thursday<td>Friday<td>Saturday</tr>
<%
leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;
int daysInMonth = dom[mm];
if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1)
++daysInMonth;
out.print("<tr>");
out.println(" ");
for (int i = 0; i < leadGap; i++)
{
out.print("<td> ");
}
for (int i = 1; i <= daysInMonth; i++)
{
out.print("<td>");
out.print(i);
out.print("</td>");
if ((leadGap + i) % 7 == 0)
{
out.println("</tr>");
out.print("<tr>");
}
}
}
%>
</tr>
</table>
</html>
please check the code and correct my mistake..
Thanks in Advance,
Venkatesh.P

