Tag <fmt:formatDate> định dạng ngày tháng theo nhiều cách.
Thuộc tính
| Attribute | Description | Required | Default |
|---|---|---|---|
| value | Giá trị ngày hiển thị | Yes | None |
| type | DATE, TIME, hoặc BOTH | No | date |
| dateStyle | FULL, LONG, MEDIUM, SHORT, DEFAULT | No | default |
| timeStyle | FULL, LONG, MEDIUM, SHORT, DEFAULT | No | default |
| pattern | Pattern format tùy chỉnh | No | None |
| timeZone | Múi giờ ngày hiển thị | No | Default time zone |
| var | Tên biến lưu ngày đã format | No | Print to page |
| scope | Phạm vi biến lưu ngày | No | page |
Thuộc tính pattern cho phép xử lý ngày chính xác hơn. Các ký hiệu pattern:
| Code | Mục đích | Ví dụ |
|---|---|---|
| G | Era designator | AD |
| y | Năm | 2002 |
| M | Tháng | April & 04 |
| d | Ngày trong tháng | 20 |
| h | Giờ (12h) | 12 |
| H | Giờ (24h) | 0 |
| m | Phút | 45 |
| s | Giây | 52 |
| S | Millisecond | 970 |
| E | Ngày trong tuần | Tuesday |
| D | Ngày trong năm | 180 |
| F | Ngày trong tuần của tháng | 2 (2nd Wed in month) |
| w | Tuần trong năm | 27 |
| W | Tuần trong tháng | 2 |
| a | AM/PM indicator | PM |
| k | Giờ (12h) | 24 |
| K | Giờ (24h) | 0 |
| z | Time zone | Central Standard Time |
| ' | Escape cho text | |
| '' | Single quote |
Ví dụ
<%@ taglib prefix = "c" uri = "https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "https://java.sun.com/jsp/jstl/fmt" %>
<html>
<head> <title>JSTL fmt:dateNumber Tag</title> </head>
<body>
<h3>Number Format:</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>
</body>
</html>
Kết quả:
Date Format: Formatted Date (1): 14:27:18 Formatted Date (2): 23-Sep-2010 Formatted Date (3): 23-Sep-2010 14:27:18 Formatted Date (4): 23/09/10 14:27 Formatted Date (5): 23-Sep-2010 14:27:18 Formatted Date (6): 23 September 2010 14:27:18 GST Formatted Date (7): 2010-09-23