Tag <fmt:parseDate> แปลง String เป็น Date object ตาม pattern/format ที่กำหนด
Attributes
| Attribute | คำอธิบาย | Required | Default |
| value | Date string ที่จะ parse | No | Body |
| type | DATE, TIME, หรือ BOTH | No | date |
| dateStyle | FULL, LONG, MEDIUM, SHORT, DEFAULT | No | default |
| timeStyle | FULL, LONG, MEDIUM, SHORT, DEFAULT | No | default |
| pattern | Custom pattern (SimpleDateFormat pattern) | No | - |
| parseLocale | Locale สำหรับ parsing | No | Default locale |
| timeZone | Time zone ของวันที่ | No | Default time zone |
| var | Variable name เก็บ Date object ที่ parse ได้ | No | Print to page |
| scope | Scope ของ var | No | page |
ตัวอย่าง: Parse วันที่ไทย dd-MM-yyyy
<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="https://java.sun.com/jsp/jstl/fmt" %>
<html>
<body>
<h3>Date Parsing</h3>
<c:set var="dateStr" value="20-10-2010"/>
<fmt:parseDate value="${dateStr}" var="parsedDate" pattern="dd-MM-yyyy"/>
<p>Parsed Date: <c:out value="${parsedDate}"/></p>
</body>
</html>
ผลลัพธ์
Date Parsing:
Parsed Date: Wed Oct 20 00:00:00 ICT 2010
Pattern Characters (SimpleDateFormat)
| y | Year | 2024 |
| M | Month | 10, Oct, October |
| d | Day of month | 20 |
| H | Hour (0-23) | 14 |
| h | Hour (1-12) | 02 |
| m | Minute | 30 |
| s | Second | 59 |
| a | AM/PM | PM |
| z | Time zone | ICT |
ข้อควรระวัง
- Pattern ต้อง match input string อย่างแม่นยำ
- Parse ผิดจะ throw exception (จับได้ด้วย <c:catch>)
- Time zone สำคัญสำหรับ date-time strings
- Java 8+: ควรใช้
java.time.format.DateTimeFormatter ใน controller แทน