A Jsp+javabeans implementation of the Web calendar program, mainly using the Calendar class.
Monthbean.java
Package test;
Import Java.text.DateFormat;
Import Java.text.DateFormatSymbols;
Import java.util.*;
public class Monthbean {
Public final static int[]daysinmonth={31,28,31,30,31,30,31,31,30,31,30,31};
public String []monthnames=null;
protected DateFormatSymbols symbols=null;
Protected Calendar cal=calendar.getinstance ();
protected int Today=cal.get (calendar.day_of_month);
protected int Month=cal.get (calendar.month);
protected int Year=cal.get (calendar.year);
Public Monthbean () {
Symbols=new DateFormatSymbols ();
Monthnames=symbols.getmonths ();
}
public void setmonth (int montharg) {
This.month=montharg;
}
public void setyear (int yeararg) {
This.year=yeararg;
}
public void Setmonth (String montharg) {
try{
int Month=integer.parseint (MONTHARG);
Setmonth (month);
}catch (Exception exc) {
}
}
public void Setyear (String yeararg) {
try{
int Year=integer.parseint (YEARARG);
Setyear (year); }catch (Exception exc) {
}
}
Public String Getmonthname () {
Return Monthnames[cal.get (Calendar.month)];
}
public int Gettoday () {
return to today;
}
public int Getstartcell () {
Calendar beginofmonth=calendar.getinstance ();
Beginofmonth.set (year,month,0);
Return Beginofmonth.get (Calendar.day_of_week);
}
public int Getendcell () {
Cal.set (year,month,1); int Endcell=daysinmonth[month]+getstartcell ()-1;
if (month==calendar.february && (GregorianCalendar) cal). Isleapyear (year)) {
endcell++;
}
return Endcell;
}
public void Update () {
Cal.set (this.year,this.month,1);
}
Public String getdayname (int day,boolean longformat) {
if (Longformat)
return symbols.getweekdays () [Day];
return symbols.getshortweekdays () [Day];
}
}
month.jsp
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "Test.monthbean"%>
<jsp:usebean id= "cal" class= "Test.monthbean" scope= "Request"/>
<%
int Today=cal.gettoday ();
%>
<title>
Calendar
</title>
<body bgcolor= "Silver" ><br><br><br>
<table border= "1" align= "center" width= "40%" height= "a" ><tr bgcolor= "Silver" >
<TD colspan= "7" > <font size= "+2" >
<%=cal.getmonthname ()%>
</font></td></tr>
<tr bgcolor= "BLACK" align= "center" >
<% for (int i=1;i<8;i++) {
Out.print ("<td width=/" 70/">");
Out.print ("<font size/" -1/"color=/" white/">");
Out.print (Cal.getdayname (i,true));
Out.print ("</front> </td>");
}%>
</tr>
<% int Startcell=cal.getstartcell ();
int Endcell=cal.getendcell ();
for (int cellno=0,day=1;cellno<42;cellno++) {
if (cellno%7==0) {out.print ("<tr>");}
Out.print ("<td valign=top height=57");
if (cellno<startcell| | Cellno>endcell) {
Out.print ("bgcolor=/" gray/">"); }
else {
if (day==today) {//If the background color is set to green for the current date
Out.print ("bgcolor=/" green/">");
}
Else
Out.print (">");
%>
<%=day%>
<%
day++;
}
Out.println ("</td>");
if (cellno+1%7==0) {
Out.print ("</tr>"); }
}//end for Loop
%>
</table>
</body>
The effect of the following figure: