jsp学习之--一个很好的日历代码

CalendarBean.java

package red.star; import java.util.*; public class CalendarBean { String calendar = null; int year = 1,month = -1; public void setYear(int year) { this.year = year; } public int getYear() { return year; } public void setMonth(int month) { this.month = month; } public int getMonth() { return month; } public String getCalendar() { StringBuffer buffer = new StringBuffer(); Calendar rili = Calendar.getInstance(); rili.set(year,month-1,1);//將日曆翻到year年month月1日,注意0表示一月,以此類推11表示12月 //獲取1日是星期幾(get方法返回的值是1表示星期日,返回的值是7表示星期六) int 星期幾 = rili.get(Calendar.DAY_OF_WEEK)-1; int day = 0; if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { day = 31; } if(month==4||month==6||month==9||month==11) { day = 30; } if(month==2) { if(((year%4==0)&&(year%100!=0))||(year%400==0)) { day = 29; } else { day = 28; } } String a [] = new String[42]; for(int i=0;i<星期幾;i++) { a[i] = "**"; } for(int i=星期幾,n=1;i<星期幾+day;i++) { a[i] = String.valueOf(n); n++; } for(int i=星期幾+day;i<42;i++) { a[i] = "**"; } //用表格顯示數組 buffer.append("<table border=1>"); buffer.append("<tr>"); String weekday [] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; for(int k=0;k<7;k++) { buffer.append("<td>"+weekday[k]+"</td>"); } buffer.append("</tr>"); for(int k=0;k<42;k=k+7) { buffer.append("<tr>"); for(int j=k;j<Math.min(7+k, 42);j++) { buffer.append("<td align=center>"+a[j]+"</td>"); } buffer.append("</tr>"); } buffer.append("</table>"); calendar = new String(buffer); return calendar; } }

showCalendar.jsp

<%@ page language="java" import="java.util.*" pageEncoding="BIG5"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'showCalendar.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <jsp:useBean id="rili" class="red.star.CalendarBean" scope="request" /> <jsp:setProperty name="rili" property="*"/> <form action="" method="post" name="form"> <!-- 輸入日曆的年份:<input type="text" value="2008" name="year" size=5>--> 輸入日曆的年份: <select name="year"> <% for(int i=1900;i<2500;i++) { if(i==2010) { %> <option value=<%=i%> selected><%=i%>年</option> <% } else { %> <option value=<%=i%>><%=i%>年</option> <% } } %> </select> 選擇日曆的月份: <select name="month"> <% for(int i=1;i<13;i++) { %> <option value=<%=i%>><%=i%>月</option> <% } %> </select> <br/><input type="submit" value="提交" name="submit"> </form> <font color="blue"><jsp:getProperty name="rili" property="year"/></font>年 <font color="green"><jsp:getProperty name="rili" property="month"/></font>月的日曆 <jsp:getProperty name="rili" property="calendar" /> </body> </html>

你可能感兴趣的:(jsp,String,calendar,buffer,日历,stylesheet)