In this post, you will learn how to use the <fmt:formatDate> tag in the JSTL format tags library with code example.

We use <fmt:formatDate> tag to format the date or time information using provided styles and pattern.

 

JSTL <fmt:formatDate> Syntax:

<fmt:formatDate

  value="<string>"

  type="<string>"

  dateStyle="<string>"

  timeStyle="<string>"

  pattern="<string>"

  timeZone="<string>"

  var="<string>"

  scope="<string>"/>

 

Attributes:

Name

Required

Type

Description

    

value

True

java.lang.String

Date or time to be formatted.

type

False

java.lang.String

Determines whether date or time or both to be formatted in the given date.

dateStyle

False

java.lang.String

Formatting style for date. The date format can be specified with similar semantics in class java.text.DateFormat.

timeStyle

False

java.lang.String

Formatting style for time. The time format can be specified with similar semantics in class java.text.DateFormat.

pattern

False

java.lang.String

Pattern to be used for date and time when formatting.

timeZone

False

java.lang.String

Time zone to represent for the formatted time.

var

False

java.lang.String

Name of the variable to store the resulted formatted date or time.

scope

False

java.lang.String

Scope to store the var.

 

JSTL <fmt:formatDate> Example:

The following JSP code displays the date and time information in various formats:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>&lt;fmt:formatDate&gt; Demo</title>
</head>
<body>
<h1>&lt;fmt:formatDate&gt; Demo</h1>
<c:set var="today" value="<%=new java.util.Date()%>" />
<p>Time: <strong><fmt:formatDate type="time" value="${today}" /></strong></p>
<p>Date: <strong><fmt:formatDate type="date" value="${today}" /></strong></p>
<p>Date & Time: <strong><fmt:formatDate type="both" value="${today}" /></strong></p>
<p>Date & Time Short: 
<strong>
    <fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${today}" />
</strong>
</p>
<p>Date & Time Medium: 
<strong>
    <fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${today}" />
</strong></p>
<p>Date & Time Long: 
<strong>
    <fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${today}" />
</strong></p>
<p>Date (yyyy-MM-dd): 
<strong>
    <fmt:formatDate pattern="yyyy-MM-dd" value="${today}" />
</strong></p>
</body>
</html>

 

Output:

formatdate

 

Recommended Usage of JSTL <fmt:formatDate> tag:

The <fmt:formatDate> is very useful tag to display date and time in various formats we needed. Using the pattern we can have custom formatting options. The following table summarizes constructs on how to specify a pattern to display a date time value in this tag.

Code

Description

Example

G

Designates the era

AD

y

Represents the year

2012

M

Represents the month

December & 12

d

Represents the day of the month

21

h

Represents the hour in 12 hr format

12

H

Represents the hour in 24 hr format

22

m

Represents the minute

30

s

Represents the second

50

S

Represents the millisecond

234

E

The day of the week

Sunday

D

The day of the year

90

F

The day of the week in the month

3 (3rd Thu in the month)

w

Represents the week in the year

45

W

Represents the week in the month

4

a

AM/PM indicator

am

k

Represents the hour (12 hr time)

24

K

Represents the hour (24 hr time)

0

z

Represents time zone

IST

' (single quote)

 

Escape for text

 

'' (double quotes)

 

Single quote (escaped by another single quote)

 

 

Other JSTL Format Tags:

bundle  |  formatNumber  |  message  |  param  |  parseDate  |  parseNumber  |  requestEncoding  |  setBundle  |  setLocale  |  setTimeZone  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1Jai Kumar Sonker2019-04-10 08:39
I need only dd/mm/yyyy date format.
Quote