<fmt:formatDate> Tag
The <fmt:formatDate> tag is used to format dates in various ways.
Syntax
<fmt:formatDate
value="<string>"
type="<string>"
dateStyle="<string>"
timeStyle="<string>"
pattern="<string>"
timeZone="<string>"
var="<string>"
scope="<string>"/>
Attributes
The <fmt:formatDate> tag has the following attributes:
| Attribute | Description | Required | Default Value |
|---|---|---|---|
| value | The date to be displayed | Yes | None |
| type | 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 | Custom format pattern | No | None |
| timeZone | Time zone of the date to be displayed | No | Default time zone |
| var | Variable name to store the formatted date | No | Display on page |
| scope | Scope of the variable to store the formatted date | No | Page |
<fmt:formatDate> Tag Format Patterns
| Code | Description | Example |
|---|---|---|
| G | Era designator | AD |
| y | Year without a century. If the year without a century is less than 10, it is displayed without a leading zero. | 2002 |
| M | Month in year. A single-digit month does not have a leading zero. | April & 04 |
| d | Day in month. A single-digit day does not have a leading zero. | 20 |
| h | Hour in 12-hour format. A single-digit hour does not have a leading zero. | 12 |
| H | Hour in 24-hour format. A single-digit hour does not have a leading zero. | 0 |
| m | Minute in hour. A single-digit minute does not have a leading zero. | 45 |
| s | Second in minute. A single-digit second does not have a leading zero. | 52 |
| S | Millisecond | 970 |
| E | Day of the week | Tuesday |
| D | Day in year | 180 |
| F | Day of the week in month | 2 (the second Wednesday in the month) |
| w | Week in year | 27 |
| W | Week in month | 2 |
| a | a.m./p.m. indicator | PM |
| k | Hour in 12-hour format | 24 |
| K | Hour in 24-hour format | 0 |
| z | Time zone | Central Standard Time |
| ' | Escape for text | |
| '' | Single quote |
Example Demonstration
<%@ 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</title>
</head>
<body>
<h3>Date Formatting:</h3>
<c:set var="now" value="<%=new java.util.Date()%>" />
<p>Date Format (1): <fmt:formatDate type="time"
value="${now}" /></p>
<p>Date Format (2): <fmt:formatDate type="date"
value="${now}" /></p>
<p>Date Format (3): <fmt:formatDate type="both"
value="${now}" /></p>
<p>Date Format (4): <fmt:formatDate type="both"
dateStyle="short" timeStyle="short"
value="${now}" /></p>
Date Formatting (5): <fmt:formatDate type="both"
dateStyle="medium" timeStyle="medium"
value="${now}" />
Date Formatting (6): <fmt:formatDate type="both"
dateStyle="long" timeStyle="long"
value="${now}" />
Date Formatting (7): <fmt:formatDate pattern="yyyy-MM-dd"
value="${now}" />
</body>
</html>
Example Output:
Date Formatting:
Date Formatting (1): 11:19:43
Date Formatting (2): 2016-6-26
Date Formatting (3): 2016-6-26 11:19:43
Date Formatting (4): 16-6-26 11:19 AM
Date Formatting (5): 2016-6-26 11:19:43
Date Formatting (6): June 26, 2016 11:19:43 AM
Date Formatting (7): 2016-06-26