Open In App

JSTL Formatting <fmt:formatDate> Tag

In developing JSTL applications, we used date and time as one of the representative components. But in some cases, the date or time which is displayed is to be more sorted and in a proper format. So to solve this, we can use the JSTL Formatting <fmt:formatDate> Tag.

In this article, we will be discussing the Syntax along with the parameters of this tag. We will also see its additional attributes table and lastly the example demonstrating the actual implementation.



Syntax of <fmt:formatDate> Tag

<fmt:formatDate value="yourDateObject" 
pattern="yourDateFormatPattern"
var="formattedDate"
/>

Where,

Attributes of JSTL <fmt:formatDate> Tag

Table provides a description of attributes present in <fmt:formatDate> tag with proper description.



Attribute

Description

Required

Default

value

This is the date value to be displayed

Yes

None

type

This is the type parameter which can be DATE, TIME, or BOTH

No

date

dateStyle

FULL, LONG, MEDIUM, SHORT, or DEFAULT

No

default

timeStyle

FULL, LONG, MEDIUM, SHORT, or DEFAULT

No

default

pattern

This is the custom formatting pattern for a date.

No

None

timeZone

This is the parameter to show the Time zone of the displayed date

No

default time zone

var

Variable in which the formatted data will be stored

No

None

scope

Scope in which the variable is stored (page, request, session, or application)

No

page

Example of JSTL Formatting <fmt:formatDate> Tag

In this example we will discuss how to create any date and time format using JSTL tag <fmt:formatDate>.




<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
<html>
    
   <head>
      <title>JSTL fmt:dateNumber Tag Example</title>
   </head>
    
   <body>
      <h3>Number Format:</h3>
      <c:set var="now" value="<%= new java.util.Date()%>" />
       
      <p>Formatted Time: <fmt:formatDate type="time" value="${now}" /></p>
      <p>Formatted Date: <fmt:formatDate type="date" value="${now}" /></p>
      <p>Formatted Date and Time: <fmt:formatDate type="both" value="${now}" /></p>
      <p>Short Format: <fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${now}" /></p>
      <p>Medium Format: <fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${now}" /></p>
      <p>Long Format: <fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${now}" /></p>
      <p>Custom Pattern (yyyy-MM-dd): <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" /></p>
     
  </body>
    
</html>

Output:

Number Format:
Formatted Time: 10:30:45 AM
Formatted Date: Nov 16, 2023
Formatted Date and Time: Nov 16, 2023 10:30:45 AM
Short Format: 11/16/23 10:30 AM
Medium Format: Nov 16, 2023 10:30:45 AM
Long Format: November 16, 2023 10:30:45 AM
Custom Pattern (yyyy-MM-dd): 2023-11-16

Output Screen for the Program:

Output

Explanation of the above Program:


Article Tags :