Date and Time formatting in JSP using JSTL

In this JSTL tutorial, let’s see how can we use dates and times with different formats in JSTL. Again will use the format taglib <%@ taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt” %> from JSTL to perform these operations.

Display the current time in JSTL

Set a date variable that will contain the value of the current Date() object from java.util package. By using

<c:set var="date" value="<%=new java.util.Date()%>" />To display only time from date object use the attribute type=”time” under the <fmt:formatDate/> tag.

<c:set var="date" value="<%=new java.util.Date()%>" />
<br/>Time: <strong><fmt:formatDate type="time" value="${date}" /></strong>

Display Current date in JSTL

To display only date from date object use the attribute type=”date” under the <fmt:formatDate/> tag.

<c:set var="date" value="<%=new java.util.Date()%>" />
<br/>Date: <strong><fmt:formatDate type="date" value="${date}" /></strong>

Display current Date and Time in JSTL

To display  date and time from date object use the attribute type=”both” under the <fmt:formatDate/> tag.

<c:set var="date" value="<%=new java.util.Date()%>" />
<br/>Date and Time: <strong><fmt:formatDate type="both" value="${date}" /></strong>

Display Date and Time according to the timeZone in JSTL

To display date and time according to the time zone from date object use the attribute type=”both” and timeZone=”GMT -1″ under the <fmt:formatDate/> tag.

<br/>Date and Time: <strong><fmt:formatDate type="both" value="${date}" timeZone="GMT -1" /></strong>

Date and time as per locale_id(country) time zone in JSTL

Set locale_id according to the country before displaying the date and time by using <fmt:setLocale value=”en_UK”/>

<fmt:setLocale value="en_UK"/>
<br/>Date and Time: <strong><fmt:formatDate type="both" value="${date}" timeZone="GMT -1" /></strong>

Date format in MM/dd/yyyy in JSTL

To display the date or time and different formats JSTL has an attribute called pattern=”MM/dd/yyyy”. In this attribute, we can define the date format as per the requirement.

Date in new format <fmt:formatDate pattern="MM/dd/yyyy" value="${date}" />

Date and Time formatting example in JSTL

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false" %>
     <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"  %> 
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Date and Time formatting in JSP using JSTL</title>
</head>
<body>

<c:set var="date" value="<%=new java.util.Date()%>" />
<br/>Date and Time: <strong><fmt:formatDate type="both" value="${date}" /></strong>

<br/>Date: <strong><fmt:formatDate type="date" value="${date}" /></strong>


<br/>Time: <strong><fmt:formatDate type="time" value="${date}" /></strong>

<br/>Date and Time: <strong><fmt:formatDate type="both" value="${date}" timeZone="GMT -1" /></strong>

<br/>
<fmt:setLocale value="en_UK"/>
<br/>Date and Time: <strong><fmt:formatDate type="both" value="${date}" timeZone="GMT -1" /></strong>

</body>
</html>