如何在JSP页面中打印当前日期和时间?
使用JSP程序,很容易获得当前日期和时间。您可以将简单的Date对象与toString()方法一起使用,以打印当前日期和时间,如下所示:
示例
<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%
Date date = new Date();
out.print( "<h2 align = \"center\">" +date.toString()+"</h2>");
%>
</body>
</html>现在让我们将代码保存在CurrentDate.jsp中,然后使用URLhttp://localhost:8080/CurrentDate.jsp调用此JSP。您将收到以下结果-
输出结果
Display Current Date & Time Mon Jun 21 21:46:49 GMT+04:00 2010
使用URLhttp://localhost:8080/CurrentDate.jsp刷新页面。每次刷新时,您都会发现以秒为单位的差异。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短