How to print current date and time in a JSP page?


With JSP program, it is very easy to get the current date and the time. You can use a simple Date object with the toString() method to print the current date and the time as follows −

Example

 Live Demo

<%@ 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>

Let us now keep the code in CurrentDate.jsp and then call this JSP using the URL http://localhost:8080/CurrentDate.jsp. You will receive the following result −

Output

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

Refresh the page with the URL http://localhost:8080/CurrentDate.jsp. You will find a difference in seconds every time you would refresh.

Updated on: 30-Jul-2019

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements