用JSP實現的行事曆程式
鐵樹
tieshu
rngguliu@163.com
2006年4月4日星期二
可顯示任意年月,當前日加有背景色,周末為紅色。本行事曆程式是本人議程管理程式中的一部分(附議程管理的效果圖)。 1 效果圖
議程管理的效果圖
2 JSP代碼
<body>
<jsp:useBean id="bean" class="general.CanlendarFace"/>
<%
String year, month, date;
year = request.getParameter("year");
month = request.getParameter("month");
date = request.getParameter("date");
out.println(bean.showHtml(year,month,date));
%>
</body>
3 JavaBean代碼
/**
* @author 鐵樹
*/
package general.calendar;
import java.util.*;
public class CanlendarFace {
public String showHtml(String year, String month, String date) {
int y, m, d;
GregorianCalendar temp= new GregorianCalendar ();//取得當前的日曆(格裡高裡曆)
try {
y = Integer.parseInt(year);
m = Integer.parseInt(month);
d = Integer.parseInt(date);
} catch (Exception e) {
y = temp.get(GregorianCalendar.YEAR); //取得年份 ;
m = temp.get(GregorianCalendar.MONTH); //取的月份
d = temp.get(GregorianCalendar.DAY_OF_MONTH); //取的當前日
}
if (y<1900 ||y>9999) y=temp.get(GregorianCalendar.YEAR); //取得年份 ;
if (m==-1){
y--;
m=11;
}else if(m==12){
y++;
m=0;
}else if(m<-1 || m>12){
m = temp.get(GregorianCalendar.MONTH); //取的月份
}
if (d<1 || d>31) d=1;//預設日
Calendar thisMonth = Calendar.getInstance();
thisMonth.set(y, m, d);
thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
thisMonth.set(Calendar.DAY_OF_MONTH, 1);
int firstIndex = thisMonth.get(Calendar.DAY_OF_WEEK) - 1;
int maxIndex = thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
String[] days = new String[42];
for (int i = 0; i < 42; i++) { //初始化表格式資料
days[i] = " ";
}
for (int i = 0; i < maxIndex; i++) { //初始化具有實際日期值得部分
days[firstIndex + i] = String.valueOf(i + 1);
}
String face = "";
face += "<table id=/"t1/" width=/"91%/" border=/"1/" align=/"center/" cellpadding=/"2/" cellspacing=/"1/" bordercolordark=/"#94CCF6/" bordercolorlight=/"#fefefe/">";
face += "<tr>";
face += "<td height=/"35/" colspan=/"7/" align=/"center/" bgcolor=/"#CAE8FF/">";
face += "<a href=/"daily.jsp?year="+(y-1)+"&month="+m+"&date=1/">上一年</a> ";
face += "<a href=/"daily.jsp?year="+y+"&month="+(m-1)+"&date=1/">上一月</a>";
face += "<strong class=/"STYLE4/"> " + y + " 年 " + (m +1) + " 月 </strong>";//"+d +" 日
face += "<a href=/"daily.jsp?year="+y+"&month="+(m+1)+"&date=1/">下一月</a> ";
face += "<a href=/"daily.jsp?year="+(y+1)+"&month="+m+"&date=1/">下一年</a>";
face += "</td></tr>";
face += "<tr>";
face += "<td width=/"14%/" align=/"center/" bgcolor=/"#C8E1FA/"><span class=/"STYLE3/">日</span></td>";
face += "<td width=/"14%/" align=/"center/" bgcolor=/"#C8E1FA/">一</td>";
face += "<td width=/"14%/" align=/"center/" bgcolor=/"#C8E1FA/">二</td>";
face += "<td width=/"14%/" align=/"center/" bgcolor=/"#C8E1FA/">三</td>";
face += "<td width=/"14%/" align=/"center/" bgcolor=/"#C8E1FA/">四</td>";
face += "<td width=/"14%/" align=/"center/" bgcolor=/"#C8E1FA/">五</td>";
face += "<td align=/"center/" bgcolor=/"#C8E1FA/"><span class=/"STYLE3/">六</span></td>";
face += "</tr>";
for (int j = 0; j < 6; j++) {
face += "<tr id=/"line"+ j +"/">";
String status = "status=yes,scrollbars=yes,resizable=yes,width=640,height=480";
for (int i = j * 7; i < (j + 1) * 7; i++) {
if (i==35 && days[i].equals(" ")){
face += "<script>line5.style.display=/"none/"</script>";//"<td> </td>";
break;
}
if (days[i].equalsIgnoreCase(String.valueOf(temp.get(GregorianCalendar.DAY_OF_MONTH)))&& y == temp.get(GregorianCalendar.YEAR) && m == temp.get(GregorianCalendar.MONTH)){
face += "<td bgcolor=/"#E7FEE8/" height=/"50/" align=/"center/" <span class=/"STYLE10/">";
}else if ((i%7)==0 || ((i+1)%7)==0){
face += "<td height=/"50/" align=/"center/" <span class=/"STYLE6/">";
}else{
face += "<td height=/"50/" align=/"center/" <span class=/"STYLE7/">";
}