如何在 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 中,然后使用网址 https://:8080/CurrentDate.jsp 调用此 JSP。你将收到以下结果 −

输出

Display Current Date & Time
Mon Jun 21 21:46:49 GMT+04:00 2010

使用网址 https://:8080/CurrentDate.jsp 刷新页面。每次刷新时,你都会发现秒数有差异。

更新于: 30-Jul-2019

13K+ 浏览量

开启你的职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.