單純採用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每一秒重新整理一次,秒數+1
var 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>