单纯采用js,new Date(),页面上实时显示的是客户端的时间。
<div id="showDateTime"><script type="text/javascript" language="javascript"> setInterval("showDateTime.innerHTML='当前时间:'+new Date().toLocaleString();",1000);</script></div>
必须使用java+js,代码如下:
<%@ page contentType="text/html; charset=UTF-8"%><%@ page import="java.util.*"%><%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%><%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%><%@ taglib uri="http://myfaces.apache.org/extensions" prefix="x"%><%@ taglib uri="http://www.ccb.cn/xmdc" prefix="xmdc"%><%@ taglib prefix="ig" uri="http://www.infragistics.com/faces/netadvantage"%><% Calendar cld = Calendar.getInstance(); cld.setTime(new Date()); int nian = cld.get(Calendar.YEAR); int yue = cld.get(Calendar.MONTH); int ri = cld.get(Calendar.DATE); int si = cld.get(Calendar.HOUR_OF_DAY); int fen = cld.get(Calendar.MINUTE); int miao = cld.get(Calendar.SECOND);%><xmdc:panelGrid columns="2" width="100%" columnClasses="left,right" styleClass="top_bg"> <xmdc:graphicImage url="/supres/images/logo/logo1.gif" height="51" /> <xmdc:panelGrid styleClass="state"> <x:panelGroup> <xmdc:outputText value=" 机构:#{SessionBean.userInfo.orgName}" title="#{SessionBean.userInfo.orgId}" /> <xmdc:outputText value=" | 用户: #{SessionBean.userInfo.name}" /> <f:verbatim> <div id="showDateTime"><SCRIPT language=javascript> <!-- //gjg add //先用java的date取出服务器时间,然后使用js每一秒刷新一次,秒数+1var y = "<%=nian%>"; var m = "<%=yue%>"; var d = "<%=ri%>"; var h = "<%=si%>"; var f = "<%=fen%>"; var s = "<%=miao%>"; //这句很重要 //转换成js的日期格式//在此处填写服务器当前时间var now = new Date(y,m,d,h,f,s);function CurentTime(){ var mm = now.getMinutes(); var ss = now.getTime() % 60000;ss = (ss - (ss % 1000)) / 1000; var clock = now.getHours() +':'; if (mm < 10) clock += '0'; clock += mm+':'; if (ss < 10) clock += '0'; return(clock + ss); } function refresh(){ document.getElementById("showDateTime").innerHTML = now.getYear()+"年"+(now.getMonth()+1)+"月"+now.getDate()+"日 "+ CurentTime();now.setSeconds(now.getSeconds()+1);} setInterval('refresh()',1000); //--> </SCRIPT></div> </f:verbatim> <xmdc:outputLink value="/laa/Logout.jsf"> <xmdc:outputText value="【退出系统】" /> </xmdc:outputLink> </x:panelGroup> </xmdc:panelGrid></xmdc:panelGrid>
<xmdc:panelGrid width="100%" border="0" cellspacing="0" cellpadding="0" columns="2" styleClass="menu"> <xmdc:panelGrid columns="3" border="0" cellspacing="0" cellpadding="0" style="float:left"> <ig:menu id="menu" binding="#{userMenu}"></ig:menu> </xmdc:panelGrid></xmdc:panelGrid>
另一个可以在server上直接运行的jsp:
<%@ page contentType="text/html; charset=GB2312"%><%@ page import="java.util.*,java.text.*"%><%@ page language="java"%><HTML><HEAD><%java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat( "H:m:s"); String ServerTime = formatter.format(new Date());
%><Script Language="JavaScript"> var t = "<%=ServerTime%>"; var tt = t.split(":"); var hour = parseInt(tt[0]); var minute = parseInt(tt[1]); var second = parseInt(tt[2]); function writeTime(){ second ++; if(second>59){ second = 0; minute ++; } if(minute > 59){ minute = 0; hour ++; } if(hour > 23){hour = 0;} var s = (hour<10?"0":"") + hour + ":" + (minute<10?"0":"") + minute + ":" + (second<10?"0":"") + second; document.all("spanTime").innerHTML = s; } t = setInterval("writeTime()", 1000); </script></HEAD><BODY topmargin="0" leftmargin="2" rightmargin="0" bottommargin="0">现在时刻:<b><SPAN id="spanTime"></SPAN></b></body></HTML>