//后台生成日期的类
/**
*<p>项目名称: drawtwo</p>
*<p>文件名称: CalendarBean</p>
*<p>版权所有: 版权所有(C)2008-2010</p>
*<p>公 司: </p>
*<p>编写日期: 2008-7-26下午12:04:21</p>
*<p>作 者: huangtao</p>
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author huangtao
*
*/
public class CalendarBean
{
String result = null; //变量的声明
String today = null;
int year = 2008 , month = 9;
public int getMonth()
{
return month;
}
public void setMonth(int month)
{
this.month = month;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String getResult()
{
StringBuffer sb = new StringBuffer();
Calendar ca = Calendar.getInstance();//获得一个日历的对象实例
int dates = ca.get(Calendar.DAY_OF_MONTH);//当天的号数
ca.set(year , month-1 , 8);//将日历设置为2008年8月8日
int date = ca.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;//润年29天
}
else
{
day = 28;//平年28天
}
}
String[] a = new String[42];
for(int i = 0 ; i<date ; i++)
{
a[i] = "";
}
for(int j = date , count = 1 ; j<date+day ; j++)
{
if (count<=9) a[j] = ""+String.valueOf(count);
else a[j] = String.valueOf(count);
count++;
}
if ((date+day)<43)
{
int len = date+day;
for(int i = len ; i<42 ; i++)
{
a[i] = "";
}
}
sb.append("<table border=3 border=1 align=center width=100% style=\"background-color:#ffddaa;\"");
sb.append("<tr>");
String[] week = {"星期日" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六"};
for(int k = 0 ; k<7 ; k++)
{
sb.append("<td>"+week[k]+"</td>");
}
sb.append("</tr>");
for(int j = 0 ; j<42 ; j = j+7)
{
sb.append("<tr>");
for(int f = j ; f<j+7 ; f++)
{
if (String.valueOf(dates).equals(a[f]))
{
sb.append("<td style=\"color: blue;\">"+a[f]+"</td>");
}
else
{
sb.append("<td>"+a[f]+"</td>");
}
}
sb.append("</tr>");
}
sb.append("</table>");
result = new String(sb);
return result;
}
public String getToday()
{
Calendar catoday = Calendar.getInstance();//获得当天的日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日");
Date datoday = catoday.getTime();
return sdf.format(datoday);
}
}
//前台显示日历的页面
<%@ page language="java" import="java.util.List,java.util.ArrayList;"
pageEncoding="UTF-8"%>
<%@ include file="/common/title.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
<%
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>日历显示页面</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="jsp与javabean,日历,calendar">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%
if (request.getProtocol().compareTo("HTTP/1.0")==0)
{
response.setHeader("Pragma" , "no-cache");
}
else if (request.getProtocol().compareTo("HTTP/1.1")==0)
{
response.setHeader("Cache-Control" , "no-cache");
}
response.setDateHeader("Expires" , 0);
%>
<%
List<Long> yearList = new ArrayList<Long>();
for(int i = 0 ; i<=2999 ; i++)
{
yearList.add(Long.valueOf(i));
}
List<Long> monthList = new ArrayList<Long>();
for(int i = 1 ; i<13 ; i++)
{
monthList.add(Long.valueOf(i));
}
pageContext.getRequest().setAttribute("yearList" , yearList);
pageContext.getRequest().setAttribute("monthList" , monthList);
%>
<script type="text/javascript">
function showTime()
{
var today= new Date()
var n_day = today.getDay();
switch(n_day)
{
case 0:
{
strDate = "星期日";
}
break;
case 1:
{
strDate = "星期一";
}
break;
case 2:
{
strDate ="星期二";
}
break;
case 3:
{
strDate = "星期三";
}
break;
case 4:
{
strDate = "星期四";
}
break;
case 5:
{
strDate = "星期五";
}
break;
case 6:
{
strDate = "星期六";
}
break;
case 7:
{
strDate = "星期日";
}
break;
}
var hours=today.getHours()
var minutes=today.getMinutes()
var seconds=today.getSeconds()
if (eval(hours) <10) {hours="0"+hours}
if (eval(minutes) < 10) {minutes="0"+minutes}
if (seconds < 10) {seconds="0"+seconds}
thistime = hours+":"+minutes+":"+seconds+" "+strDate;
document.getElementById("time").value=thistime;
window.setTimeout("showTime()",1000);
}
</script>
<body οnlοad="showTime();">
<jsp:useBean id="calendar" scope="request"
class="com.guokang.util.CalendarBean" />
<form action="" method="post" name="calendarform">
<table border="1" align="center" width="80%"
style="background-color: #ffddaa;">
<tr align="center">
<td colspan="2">
请选则你要查看的日历
</td>
</tr>
<tr align="center">
<td colspan="2">
年份 月份
</td>
</tr>
<tr align="center">
<td colspan="2">
<select id="year" name="year" size="1">
<logic:iterate id="yearItem" name="yearList">
<option value="<%=yearItem%>">
<%=yearItem%>
</option>
</logic:iterate>
</select>
<select id="month" name="month" size="1">
<%--<logic:iterate id="monthItem" name="monthList">
<option value="<%=monthItem%>">
<%=monthItem%>
</option>
</logic:iterate>
--%>
<c:forEach items="${monthList}" var="monthItem" begin="0"
end="12" step="1">
<option value="${monthItem}">
${monthItem}
</option>
</c:forEach>
</select>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="submit" value="查看"
style="background-color: #ffddaa;">
</td>
</tr>
<tr align="left" style="background-color: #ffddaa;">
<td>
当前显示的日历是公元:
<jsp:setProperty name="calendar" property="*" />
<font color="red"> <jsp:getProperty name="calendar"
property="year" /> </font>年
<font color="blue"> <jsp:getProperty name="calendar"
property="month" /> </font>月的日历 今天日期是公元:
<jsp:getProperty name="calendar" property="today" />
<input type="text" id="time" name="time" size="16"
readonly="readonly" border="0"
style="background-color: white; border-left: 0; border-right: 0; border-top: 0; border-bottom: 0; font-size: 16; color: red; background-color: #ffddaa;">
<hr width="100%" color="red">
<jsp:getProperty name="calendar" property="result" />
</td>
</tr>
</table>
</form>
</body>
</html>