JSP/Servlet | JSTL(JSP Standard Tag Library) | Format fmt
The fmt format tag library is used when displaying locale, message formats, numbers, dates, and similar values.
Directive declaration
To use formatting, you need the JSTL format declaration at the top of the JSP page.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
formatDate
This tag expresses a date format.
<fmt:formatDate value="date"
[type="{time|date|both}"]
[dateStyle="{default|short|medium|long|full}"]
[timeStyle="{default|short|medium|long|full}"]
[pattern="customPattern"]
[timeZone="timeZone"]
[var="varName"]
[scope="{page|request|session|application}"]/>
Attribute descriptions
| Attribute | Type | Description | Required | Default |
|---|---|---|---|---|
| value | java.util.Date | Input Date value to display | Required | |
| type | String | Specifies whether the displayed data is time, date, or both | date | |
| dateStyle | String | Predefined date format. Follows the syntax defined in java.text.DateFormat. Used when type is omitted, date, or both | ||
| timeStyle | String | Predefined time format. Used when type is time or both | ||
| pattern | String | Custom format style | ||
| timeZone | String or java.util.TimeZone | Time zone shown during formatting | ||
| var | String | Variable name in the scope that stores the formatted output result string | ||
| scope | String | Scope of var |
The pattern attribute is used to specify more precise date handling.
| Code | Purpose | Display example |
|---|---|---|
| G | The era designator | AD |
| y | The year | 2018 |
| M | The month | 7 |
| d | The day of the month | 6 |
| h | The hour (12-hour time) | 12 |
| H | The hour (24-hour time) | 0 |
| m | The minute | 29 |
| s | The second | 59 |
| S | The millisecond | 494 |
| E | The day of the week | Fri |
| D | The day of the year | 187 |
| F | The day of the week in the month | 1 |
| w | The week in the year | 27 |
| W | The week in the month | 1 |
| a | The a.m./p.m. indicator | AM |
| k | The hour (12-hour time) | 24 |
| K | The hour (24-hour time) | 0 |
| z | The time zone | KST |
| ' | The escape for text | |
| '' | The single quote |
Usage example
<%@ 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" %>
<!DOCTYPE html">
<html>
<head>
<title>JSTL fmt formatDate</title>
</head>
<body>
<h1>JSTL fmt formatDate</h1>
<c:set var="now" value="<%=new java.util.Date()%>" />
<p><fmt:formatDate value="${now}" type="date"/></p>
<p><fmt:formatDate value="${now}" type="time"/></p>
<p><fmt:formatDate value="${now}" type="both"/></p>
<p><fmt:formatDate value="${now}" type="both" dateStyle="default" timeStyle="default"/></p>
<p><fmt:formatDate value="${now}" type="both" dateStyle="short" timeStyle="short"/></p>
<p><fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="medium"/></p>
<p><fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long"/></p>
<p><fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/></p>
<p><fmt:formatDate value="${now}" type="both" pattern="yyyy MM dd HH:mm:ss E"/></p>
</body>
</html>
The result is displayed as follows.
JSTL fmt formatDate
2018. 7. 6
12:19:48 AM
2018. 7. 6 12:19:48 AM
2018. 7. 6 12:19:48 AM
18. 7. 6 12:19 AM
2018. 7. 6 12:19:48 AM
July 6, 2018 12:19:48 AM
Friday, July 6, 2018 12:19:48 AM KST
2018 07 06 00:19:48 Friday