关于在jsp页面显示当前时间及动态显示

来源:互联网 发布:设置有限数据列 编辑:程序博客网 时间:2024/04/29 02:23

通过网罗资料,整理出来的:

 

1、<%--先导入两个包java.util.*和java.text.*--%>

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.* "import ="java.text.*"%>

<html>

<head><title>显示当前时间</title></head>

<%--显示时间的格式为:"yyyy-MM--dd HH:mm:ss "--%>

<% Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

%>

<body>

当前时间是:<%=sdf.format(date)%>

</body>

</html>

=============以上显示当前时间就完了,不能动态显示

注意:%@page %中import和language等之间要有空格,不然报错误:The JSP specification requires that an attribute name is preceded by whitespace

2、动态显示当前时间

<script type="text/javascript"> /*页面动态显示当前的时间OK*/
function getCurrentTime(){
    var now = new Date();
    var time=document.getElementById("showTime").value;
    time=now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
    time+=" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
     document.getElementById("showTime").innerHTML=time;
    }
    window.setInterval("getCurrentTime()",1000);
</script>

<div id="time" >当前时间是:<span id="showTime"></span></div>

 

以上经本人调试验证,都可以实现啊
原创粉丝点击