jstl <fmt:formatDate>标签

<fmt:formatDate>标签用于以各种方式格式化日期。

属性

<fmt:formatDate>标签具有以下属性 -

属性 描述 必需 默认
value 要显示的日期值
type DATE, TIMEBOTH DATE
dateStyle FULL, LONG, MEDIUM, SHORTDEFAULT default
timeStyle FULL, LONG, MEDIUM, SHORTDEFAULT default
pattern 自定义格式模式
timeZone 显示日期的时区 默认时区
var 用于存储格式化日期的变量名称 在页面内打印
scope 存储格式化日期的变量范围 page
  • pattern属性用于指定更精确的日期处理 -
代码 目的 示例
G 时代标志符 AD
y 年份 2018
M 月份 April 或 04
d 月中的一天 2031
h 小时(12小时) 12
H 小时(24小时) 18,23
m 分钟 45
s 秒钟 58
S 毫秒 970
E 星期几 Tuesday
D 一年中的第几天 180
F 这个月的星期几 2(在一个月的第2个星期三)
w 一年中的第几周 27
W 一个月中的第几周 2
a a.m./p.m.指示符 PM
k 小时(12小时) 12
K 小时(24小时) 0
z 时区 中央标准时间
' 文字的转义
'' 单引号

示例

文件:fmt_formatDate.jsp -

<%@ 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 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=UTF-8">
<title>日期格式化示例</title>
</head>
<body>
    <div style="margin: auto; width: 80%">
        <h3>日期格式化示例:</h3>
      <c:set var = "now" value = "<%= new java.util.Date()%>" />

      <p>Formatted Date (1): <fmt:formatDate type = "time" 
         value = "${now}" /></p>

      <p>Formatted Date (2): <fmt:formatDate type = "date" 
         value = "${now}" /></p>

      <p>Formatted Date (3): <fmt:formatDate type = "both" 
         value = "${now}" /></p>

      <p>Formatted Date (4): <fmt:formatDate type = "both" 
         dateStyle = "short" timeStyle = "short" value = "${now}" /></p>

      <p>Formatted Date (5): <fmt:formatDate type = "both" 
         dateStyle = "medium" timeStyle = "medium" value = "${now}" /></p>

      <p>Formatted Date (6): <fmt:formatDate type = "both" 
         dateStyle = "long" timeStyle = "long" value = "${now}" /></p>

      <p>Formatted Date (7): <fmt:formatDate pattern = "yyyy-MM-dd" 
         value = "${now}" /></p>
    </div>

</body>
</html>

这将产生以下结果 -

日期格式化示例:
Formatted Date (1): 3:18:43
Formatted Date (2): 2017-10-24
Formatted Date (3): 2017-10-24 3:18:43
Formatted Date (4): 17-10-24 上午3:18
Formatted Date (5): 2017-10-24 3:18:43
Formatted Date (6): 2017年10月24日 上午03时18分43秒
Formatted Date (7): 2017-10-24

上一篇: Jstl教程 下一篇: JSP+MySQL实例