JavaBean在JSP中显示时间

创建DateTimeBean的类,将其放置于org.caiduping.bean的包中,实现时间,星期的封装。

package org.caiduping.bean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class DateTimeBean {
private String dateTime;
private String week;
//Calendar对象
private Calendar calendar=Calendar.getInstance();
/*
* 获取日期和时间
*
* */
public String getdateTime(){
//获取当前时间
Date currDate=Calendar.getInstance().getTime();
//实例化SimpleDateFormat
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");
//格式化日期时间
dateTime=sdf.format(currDate);
return dateTime;
}
/*
* 获取星期几
*
* */
public String getweek(){
//定义数组
String[] weeks={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
//获取一星期的某天
int index=calendar.get(Calendar.DAY_OF_WEEK);
//获取星期几
week=weeks[index-1];
return week; }
}

  

而index.jsp则实例化DateTimeBean对象:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
<style type="text/css">
#clock{
width:500px;
height:100px;
padding-top: 20px;
border:solid 5px black;
}
#week{
padding-top: 20px; }
</style>
<meta http-equiv="Refresh" content="1">
<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> <body>
<jsp:useBean id="date" class="org.caiduping.bean.DateTimeBean"></jsp:useBean>
<div id="clock">
<div id="time">
<jsp:getProperty property="dateTime" name="date"/>
</div>
<div id="week">
<jsp:getProperty property="week" name="date"/>
</div>
</div>
</body>
</html>

  

上一篇:javaSE总结


下一篇:ArcGis Python脚本——ArcGIS 中使用的 Python 是什么版本