One of the primary things that developers need in JSP is to display the current date and time on a webpage. This tutorial will teach you how to use date and time in JSP.



The current date and time can be displayed on the JSP page using two different methods:

Method 1: Using the java.util.Date class

The first method uses the "java.util.Date class, " a built-in Java class representing a specific time point. To use this class on a JSP page, you can use the "jsp:useBean" action tag to create a new Date object and the "jsp:getProperty" action tag to retrieve the date and time from the object.

Here is an example of displaying the current date and time on a JSP page using this method:

Example:

<%@ page import="java.util.Date" %>

<jsp:useBean id="currentDate" class="java.util.Date"/>

<p>Current Date and Time:</p>
<p>
<jsp:getProperty name="currentDate" property="time"/>
</p>

Output:

Current Date and Time:
1674881959583

In the above example, the "jsp:useBean" action tag creates a new Date object and assigns it to the "currentDate" variable. The "jsp:getProperty" action tag then retrieves the "time" property of the "currentDate" object, which is the current date and time in milliseconds.

Here is another example to display the current date and time on the JSP page without using JavaBean:

Example:

<%@ page import="java.util.Date" %>
<%
  Date date = new Date();
%>

<p>The current date and time is: <%= date %></p>

Output:

The current date and time is: Sat Jan 28 10:31:05 IST 2023

Method 2: Using JSTL (JavaServer Pages Standard Tag Library)

Another way to display the current date and time in JSP is using the JSTL (JavaServer Pages Standard Tag Library). JSTL provides a set of tags that can be used to format the date and time. To use JSTL on your JSP page, you need to include the following taglib directive at the top of your page:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Once you have included the taglib directive, you can use the <fmt:formatDate> tag to format the date and time.

Here is an example of displaying the current formatted date and time on a JSP page using JSTL:

Example:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
<head>
    <title>Example of JSTL formatDate</title>
</head>
<body>
    <jsp:useBean id="now" class="java.util.Date" />
    <fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long" />
</body>
</html>

Output:

28 January 2023 at 11:09:47 am IST

In the above example, the "jsp:useBean" action tag creates a new Date object, and the "<fmt:formatDate>" tag is for to format the date and time. The tag's value attribute is set to "${now}" which is the current date and time. The type attribute is set to "both" which means to format the date and time. The "DateStyle" and "TimeStyle" attributes are set to "long," which means to display the date and time in a long format.

formatDate Attributes

The <fmt:formatDate> tag in JSTL has several attributes that can be used to customize the date and time formatting. Here is a list of some of the most commonly used attributes:

  • value: The date and time to be formatted. This attribute can be a date object, a long value (representing the number of milliseconds since January 1, 1970, 00:00:00 GMT), or a string value (in a format the class can parse).
  • pattern: The pattern to be used for formatting the date and time. This attribute uses the same syntax as the SimpleDateFormat class, which includes special characters for various parts of the date and time, such as "yyyy" for the year, "MM" for the month, "dd" for the day, "HH" for the hour, "mm" for the minutes, "ss" for the seconds, etc.
  • type: The type of date and time to be formatted. This attribute can be "date", "time", "both", or "default". The "default" value will use the type of the value attribute.
  • dateStyle: The style of the date to be formatted. This attribute can be "default", "short", "medium", "long", or "full".
  • timeStyle: The style of the time to be formatted. This attribute can be "default", "short", "medium", "long", or "full".
  • timeZone: The time zone to be used for formatting the date and time. This attribute can be a TimeZone object, a string representation of a time zone (such as "UTC" or "PST"), or the value "default" to use the time zone of the value attribute.
  • var: The name of the variable to store the formatted date and time. This attribute can be used in conjunction with the scope attribute to store the formatted date and time in a variable that can be accessed later on the JSP page.

Here is another example of displaying the date and time in JSP using JSTL's <fmt:formatDate> tag with the pattern attribute:

Example:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
<head>
    <title>Example of JSTL formatDate with pattern attribute</title>
</head>
<body>
    <jsp:useBean id="now" class="java.util.Date" />
    <fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" var="formattedDate"/>
    <p>The current date and time is: ${formattedDate}</p>
</body>
</html>

Output:

The current date and time is: 2023-01-28 22:56:32



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram