datebean
package itcast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateBean {
private String dateTime;
private String week;
private Calendar calendar = Calendar.getInstance();
/**
* 获取当前日期和时间
*
* @return 日期及时间字符串
*/
public String getDateTime() {
Date currDate = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy 年 MM 月 dd 日 HH 点 mm 分 ss 秒 ");
dateTime = sdf.format(currDate);
return dateTime;
}
/**
* 获取星期几
*
* @return 返回星期几字符串
*/
public String getWeek() {
String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
int index = calendar.get(Calendar.DAY_OF_WEEK);
week = weeks[index - 1];
return week;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
errorPage="error.jsp"%>
<%
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 'error.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">
-->
</head>
<style type="text/css">
#clock {
width: 420px;
height: 80px;
background: #E0E0E0;
font-size: 25px;
font-weight: bold;
border: solid 5px orange;
padding: 20px;
}
#week {
padding-top: 15px;
color: #0080FF;
}
</style>
<meta http-equiv="Refresh" content="1"><%-- 刷新界面 --%>
<body>
<jsp:useBean id="date" class="itcast.DateBean" scope="application"></jsp:useBean>
<div align="center">
<div id="clock">
<div id="time">
<jsp:getProperty property="dateTime" name="date" />
</div>
<div id="week">
<jsp:getProperty property="week" name="date" />
</div>
</div>
</div>
</body>
</html>