java.util.Calendar使用总结
问题:系统首页title显示的时间,用javascript读取本地时间,代码如下:
function clockon(){ var now=new Date(); var year=now.getYear(); var month=now.getMonth(); var date=now.getDate(); var day=now.getDay(); var hour=now.getHours(); var minute=now.getMinutes(); var second=now.getSeconds(); month=month+1; if(month<10){month="0"+month}; if(date<10){date="0"+date}; if(hour<10){hour="0"+hour}; if(second<10){second="0"+second}; var arr_week=new Array('周日','周一','周二','周三','周四','周五','周六'); var week=arr_week[day]; var time=year+"年"+month+"月"+date+"日"+" "+week; document.getElementById('system_time').innerHTML=time; setTimeout('clockonbak()',1000); }
设定每1000ms执行一次,读取客户机时间。
现在要求读取服务器时间,前台改用ajax调用,代码如下:
function clockon(){ jQuery.ajax( { type : "POST", datatype:"text", url : "${pageContext.request.contextPath}/simpleCRUDAction.do?methodName=queryDBdate", beforeSend:function(){ }, error:function(){ }, success : function(data) { document.getElementById('system_time').innerHTML=data; } }); }
后台调取代码如下:
public ActionForward queryDBdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //今天是 2013年4月12日 周五 Date de=WebUtils.getTimeFormDataBase(); String currentTime=""; final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" }; Calendar calendar = Calendar.getInstance(); calendar.setTime(de); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; if (dayOfWeek < 0) dayOfWeek = 0; // System.out.println(dayNames[dayOfWeek]); //System.out.println(calendar.get(Calendar.MONTH)+1); currentTime = String.valueOf(calendar.get(Calendar.YEAR)) + "年" + String.valueOf(calendar.get(Calendar.MONTH)+1) + "月" + String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + "日" + " " + dayNames[dayOfWeek]; //System.out.println("今天是" + currentTime); response.getWriter().print(currentTime); return null; }
做完也没太注意看,结果今天有人反映,月份跟实际月份少一个月。仔细查了一下sdk中Calender.get(Calendar.MONTH)的用法,记录以做标记。
----------------------------------------------------------------------------------------------------------------------
The java.util.Calendar
class is used to represent the date and time. The year, month, day, hour, minute, second, and milliseconds can all be set or obtained from a Calendar object. The default Calendar object has the current time in it.
Calendar t = Calendar.getInstance();
The following field names can be used as an argument to the Calendar.get(. . .)
method. In all of these examples, t
is a Calendar object.
Access Method | Meaning |
---|---|
t.get(Calendar.YEAR) |
int value of the year |
t.get(Calendar.MONTH) |
int value of the month (0-11) |
t.get(Calendar.DAY_OF_MONTH) |
int value of the day of the month (1-31) |
t.get(Calendar.DAY_OF_WEEK) |
int value of the day of the week (0-6) |
t.get(Calendar.HOUR) |
int value of the hour in 12 hour notation (0-12) |
t.get(Calendar.AM_PM) |
returns either Calendar.AM or Calendar.PM |
t.get(Calendar.HOUR_OF_DAY) |
int value of the hour of the day in 24-hour notation (0-24) |
t.get(Calendar.MINUTE) |
int value of the minute in the hour (0-59) |
t.get(Calendar.SECOND) |
int value of the second within the minute (0-59). |
t.get(Calendar.MILLISECOND) |
int value of the milliseconds within a second (0-999). |
月份的取值区间是从0到11,所以提取出来的值应该再加1。