一个jsp+javabeans实现的web日历程序,主要使用了calendar类!
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.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();
%>
<html>
<head>
<title>
日历
</title>
</head>
<body bgcolor="silver" ><br><br><br>
<table border="1" align="center" width="40%" height="100"><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){ //如果为当前日期,背景色设为绿色
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>
新闻热点
疑难解答