在JSP網頁上實現月曆

來源:互聯網
上載者:User
js|網頁 在JSP網頁上實現月曆

JSP、月曆

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,java.text.*" %>
<html>
<head>
<title>月曆</title>
</head>
<body style="font-size:12px">
<%!
/*聲明變數*/
String[] months = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
String[] months_cn = {"一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月"};
public final static int dom[] = {
31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31
};
%>
<%
/*處理事件*/
boolean yyok = false;
int yy = 0, mm = 0;
String yyString = request.getParameter("year");
if (yyString != null && yyString.length() >0) {
try {
yy=Integer.parseInt(yyString);
yyok=true;
}
catch (NumberFormatException e) {
out.println("年份不可用");
}
Calendar cal = Calendar.getInstance();
if (!yyok)
yy = cal.get(Calendar.YEAR);
String mmString = request.getParameter("month");
if (mmString == null) {
mm = cal.get(Calendar.MONTH);
}
else {
for (int i = 0; i < months.length; i++)
if (months[i].equals(mmString)) {
mm = i;
break;
}
}
}
%>
<form name="cal" method=post action="cal.jsp">
請選擇月份:
<select name="month">
<%
/*初始化表單*/
for (int i = 0; i < months.length; i++) {
if (i == mm)
out.print("<option selected value=January>");
else
out.print("<option value="+months[i]+">");
out.print(months_cn[i]);
out.print("</option>");
}
%>
</select>
<br>請輸入年份:
<input type="text" size="5" name="year" value="<%=yy%>">  
<input type="submit" value="顯示">
</form>
<%
int lead = 0;
%>
<table border="0" cellpadding="1" cellspacing="1" style="font-size:12px">
<tr height="20"><td colspan="7"><font color="#3399FF"><b><%= months[mm]%> <%= yy%></b></font></td></tr>
<% GregorianCalendar calendar = new GregorianCalendar(yy, mm ,1); %>
<tr><td><font color="#FF0000">Sun</font></td><td>Mon</td><td>Tue</td><td>Wed</td>
<td>Thu</td><td>Fri</td><td><font color="#FF0000">Sat</font></td></tr>
<%
/*下面是顯示月曆的代碼*/
lead = calendar.get(Calendar.DAY_OF_WEEK)-1;
int dayInMonth = dom[mm];
if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1)
++dayInMonth;
out.print("<tr>");
for(int i = 0; i < lead; i++) {
out.print("<td> </td>");
}
for(int i = 1; i <= dayInMonth; i++) {
if ((i+lead) % 7 == 0 || (i+lead) % 7 == 1)
out.print("<td align=\"center\"><font color=\"#FF0000\">"+i+"</font></td>");
else
out.print("<td align=\"center\">"+i+"</td>");
if ((lead+i) % 7 == 0) {
out.print("</tr></tr>");
}
}
out.print("</tr>");
%>
</table>
</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.