使用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中,然后使用URL http:// localhost:8080 / CurrentDate.jsp调用此JSP 。您将收到以下结果-
输出结果
Display Current Date & Time
Mon Jun 21 21:46:49 GMT+04:00 2010
使用URL http:// localhost:8080 / CurrentDate.jsp刷新页面。每次刷新时,您都会发现以秒为单位的差异。