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
<%@ 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 https://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 https://localhost:8080/CurrentDate.jsp. You will find a difference in seconds every time you would refresh.