创建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对象:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
电子时钟#clock{
width:500px;
height:100px;
padding-top: 20px;
border:solid 5px black;
}
#week{
padding-top: 20px;
}