Print current date & time - JSP Program

Q. Write a simple JSP program to print the current date and time.

Answer:

The most important use of JSP is that we can use all the method available in core java. The Date class is available in java.util package.

Below program shows how to print the current date and time. We can use simple Date object with toString() to print current date and time.

test.jsp

<html>
    <head><title>JSPApp</title></head>
    <body>
        <form>
            <fieldset style="width:20%; background-color: #ccffeb;">
                <legend><b><i>JSP Application<i><b></legend>
                <h3>Current Date and Time is :</h3>
                <% java.util.Date d = new java.util.Date();
                out.println(d.toString()); %>
            </fieldset>
        </form>
    </body>
</html>


web.xml

<web-app>
    <servlet>
        <servlet-name>xyz</servlet-name>
        <jsp-file>/test.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>xyz</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>


Output:

date time