A JSP + JavaBeans web calendar program mainly uses 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 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. getreceivweekdays () [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 ();
%>
<HTML>
<Head>
<Title>
Calendar
</Title>
</Head>
<Body bgcolor = "Silver"> <br>
<Table border = "1" align = "center" width = "40%" Height = "100"> <tr bgcolor = "Silver">
<TD colspan = "7"> & nbsp; <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 it is the current date, the background color is set to green
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>
</Html>