Open In App

JSTL Formatting <fmt:formatDate> Tag

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • tag is used to format the time and date according to any custom pattern.

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,

  • value: This is the data that is to be formatted. We can use the Date object or other other to get the date.
  • pattern: This is the customized pattern for formatting the data. According to this pattern, the date will be represented.
  • var: This is the optional attribute which is a variable to store the formatted date.

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>.

HTML




<%@ 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 of the Program

Output

Explanation of the above Program:

  • In the above example, firstly, we have retrieved the current time from the Date and Time object as java.util.Date.
  • Then, by using the fmt:formatDate tag, we format the date in various forms.
  • We have used different attributes like value, type, dateStyle, etc.
  • We have given the custom date format pattern using pattern attributes.
  • All the output is been displayed on the screen using <p> tag in HTML.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads