jsp學習之--一個很好的日曆代碼__js

來源:互聯網
上載者:User

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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.