English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

<fmt:formatDate> Tag

JSP Standard Tag Library

<fmt:formatDate> tag is used to format dates in different ways.

syntax format

<fmt:formatDate
  value="<string>"
  type="<string>"
  dateStyle="<string>"
  timeStyle="<string>"
  pattern="<string>"
  timeZone="<string>"
  var="<string>"
  scope="<string>"/>

attribute

<fmt:formatDate> tag has the following attributes:

attributedescription whether necessarydefault value
valuedate to be displayedyesnone
typeDATE, TIME, or BOTHnodate
dateStyleFULL, LONG, MEDIUM, SHORT, or DEFAULTnodefault
timeStyleFULL, LONG, MEDIUM, SHORT, or DEFAULTnodefault
patterncustom format patternnonone
timeZonetime zone displayed when showing the datenodefault time zone
varstorage variable name of formatted datenodisplayed on the page
scopestorage range of formatted log variablesnopage

<fmt:formatDate> tag format pattern

codedescription example

G

era marker

AD

y

Years not including the era. If the year not including the era is less than 10If the year does not have a leading zero, it is displayed.

2002

M

month number. A single-digit month does not have a leading zero.

April & 04

d

day of the month. A single-digit date does not have a leading zero.

20

h

12 hour in 12-hour format. A single-digit hour does not have a leading zero.

12

H

24 hour in 12-hour format. A single-digit hour does not have a leading zero.

0

m

minutes. A single-digit minute does not have a leading zero.

45

s

seconds. A single-digit second does not have a leading zero.

52

S

milliseconds

970

E

day of the week

Tuesday

D

一年中的第几天

180

F

一个月中的第几个周几

2 (一个月中的第二个星期三)

w

一年中的第几周r

27

W

一个月中的第几周

2

a

a.m./p.m. indicator

PM

k

小时(12 小时制的小时)

24

K

小时(24 小时制的小时)

0

z

时区

中部标准时间

'

 

转义文本

''

 

单引号


示例演示

<%@ 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>
<h2>Date Formatting:</h2>
<c:set var="now" value="<%=new java.util.Date()%>" />
<p>Date Formatting (1): <fmt:formatDate type="time" 
            value="${now}" /></p>
<p>Date Formatting (2): <fmt:formatDate type="date" 
            value="${now}" /></p>
<p>Date Formatting (3): <fmt:formatDate type="both" 
            value="${now}" /></p>
<p>Date Formatting (4): <fmt:formatDate type="both" 
            dateStyle="short" timeStyle="short" 
            value="${now}" /></p>
<p>Date Formatting (5): <fmt:formatDate type="both" 
            dateStyle="medium" timeStyle="medium" 
            value="${now}" /></p>
<p>Date Formatting (6): <fmt:formatDate type="both" 
            dateStyle="long" timeStyle="long" 
            value="${now}" /></p>
<p>Date Formatting (7): <fmt:formatDate pattern="yyyy"-MM-dd" 
            value="${now}" /></p>
</body>
</html>

The above example execution result is:

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 Morning11:19
Date Formatting (5) : 2016-6-26 11:19:43
Date Formatting (6) : 2016Year6Month26Day Morning11Hours19Minutes43Seconds
Date Formatting (7) : 2016-06-26

JSP Standard Tag Library