<fmt:formatDate> Tag

JSP 标准标签库JSP Standard Tag Library

The <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>"/>

Attributes

The <fmt:formatDate> tag has the following attributes:

AttributeDescriptionRequiredDefault Value
valueDate to displayYesNone
typeDATE, TIME, or BOTHnodate
dateStyleFULL, LONG, MEDIUM, SHORT, or DEFAULTnodefault
timeStyleFULL, LONG, MEDIUM, SHORT, or DEFAULTnodefault
patternCustom format patternnoNone
timeZoneTime zone for displaying the datenoDefault time zone
varVariable name to store the formatted datenoDisplay on page
scopeScope for storing the formatted date variablenoPage

<fmt:formatDate> Tag Format Patterns

CodeDescriptionExample

G

Era designator

AD

y

Year without era. If the year without era is less than 10, the year is displayed without a leading zero.

2002

M

Month number. One-digit months have no leading zero.

April & 04

d

Day of the month. One-digit dates have no leading zero.

20

h

Hour in 12-hour format. One-digit hours have no leading zero.

12

H

Hour in 24-hour format. One-digit hours have no leading zero.

0

m

Minute. One-digit minutes have no leading zero.

45

s

Second. One-digit seconds have no leading zero.

52

S

Millisecond

970

E

Day of the week

Tuesday

D

Day of the year

180

F

Day of the week in the month

2 (the second Wednesday in a month)

w

Week of the year

27

W

Week of the month

2

a

a.m./p.m. indicator

PM

k

Hour (hour in 12-hour format)

24

K

Hour (hour in 24-hour format)

0

z

Time zone

Central Standard Time

'

 

Escaped text

''

 

Single quote



Examples

<%@ 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 标签</title>
</head>
<body>
<h3>日期格式化:</h3>
<c:set var="now" value="<%=new java.util.Date()%>" />

<p>日期格式化 (1): <fmt:formatDate type="time" 
            value="${now}" /></p>
<p>日期格式化 (2): <fmt:formatDate type="date" 
            value="${now}" /></p>
<p>日期格式化 (3): <fmt:formatDate type="both" 
            value="${now}" /></p>
<p>日期格式化 (4): <fmt:formatDate type="both" 
            dateStyle="short" timeStyle="short" 
            value="${now}" /></p>
<p>日期格式化 (5): <fmt:formatDate type="both" 
            dateStyle="medium" timeStyle="medium" 
            value="${now}" /></p>
<p>日期格式化 (6): <fmt:formatDate type="both" 
            dateStyle="long" timeStyle="long" 
            value="${now}" /></p>
<p>日期格式化 (7): <fmt:formatDate pattern="yyyy-MM-dd" 
            value="${now}" /></p>

</body>
</html>

The output of the above example is:

日期格式化:

日期格式化 (1): 11:19:43

日期格式化 (2): 2016-6-26

日期格式化 (3): 2016-6-26 11:19:43

日期格式化 (4): 16-6-26 上午11:19

日期格式化 (5): 2016-6-26 11:19:43

日期格式化 (6): 2016年6月26日 上午11时19分43秒

日期格式化 (7): 2016-06-26

JSP 标准标签库JSP Standard Tag Library

Other extensions