JSP/Servlet | JSTL(JSP Standard Tag Library) | 포멧 fmt
fmt 포멧은 지역, 메시지 형식, 숫자 및 날짜형식 등 표시할 때 사용한다.
지시문 선언
포멧을 사용하려면 JSP 페이지 상단에 JSTL format 선언이 필요하다.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
formatDate
날짜 형식을 표현하는 태그이다.
<fmt:formatDate value="date" 
        [type="{time|date|both}"]
        [dateStyle="{default|short|medium|long|full}"] 
        [timeStyle="{default|short|medium|long|full}"]
        [pattern="customPattern"] 
        [timeZone="timeZone"] 
        [var="varName"]
        [scope="{page|request|session|application}"]/> 
속성 설명
| 속성 | Type | 설명 | 필수 여부 | 기본값 | 
|---|---|---|---|---|
| value | java.util.Date | 표시할 입력값 Date | 필수 | |
| type | String | 표시할 데이터가 시간, 날짜, 혹은 둘다인지 지정 | date | |
| dateStyle | String | 미리 정의된 날짜 형식. java.text.DateFormat 클래스에 정의된 문법을 따른다. type 속성이 생략되었거나 date 혹은 body일 때 사용 | ||
| timeStyle | String | 미리 정의된 시간 형식. ype 속성이 time 혹은 body일 때 사용 | ||
| pattern | String | 사용자 지정 형식 스타일 | ||
| timeZone | String 또는 java.util.TimeZone | 형식화 시간에 나타날 타임 존 | ||
| var | String | 형식 출력 결과 문자열을 담는 scope에 해당하는 변수명 | ||
| scope | String | var의 scope | 
pattern 속성은 보다 정확한 날짜 처리를 지정하는데 사용된다.
| 코드 | 사용 용도 | 표시 예제 | 
|---|---|---|
| G | The era designator | 서기 | 
| y | The year | 2018 | 
| M | The month | 7 | 
| d | The day of the month | 6 | 
| h | The hour(12-hour time) | 12 | 
| H | The hour(24-hour time) | 0 | 
| m | The minute | 29 | 
| s | The second | 59 | 
| S | The millisecond | 494 | 
| E | The day of the week | 금 | 
| D | The day of the year | 187 | 
| F | The day of the week in the month | 1 | 
| w | The week in the year | 27 | 
| W | The week in the month | 1 | 
| a | The a.m./p.m. indicator | 오전 | 
| k | The hour(12-hour time) | 24 | 
| K | The hour(24-hour time) | 0 | 
| z | The time zone | KST | 
| ' | The escape for text | |
| '' | The single quote | 
사용 예제
<%@ 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">
<html>
    <head>
        <title>JSTL fmt formatDate</title>
    </head>
    <body>
    <h1>JSTL fmt formatDate</h1>
        <c:set var="now" value="<%=new java.util.Date()%>" />
        <p><fmt:formatDate value="${now}" type="date"/></p>
        <p><fmt:formatDate value="${now}" type="time"/></p>
        <p><fmt:formatDate value="${now}" type="both"/></p>
        <p><fmt:formatDate value="${now}" type="both" dateStyle="default" timeStyle="default"/></p>
        <p><fmt:formatDate value="${now}" type="both" dateStyle="short" timeStyle="short"/></p>
        <p><fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="medium"/></p>
        <p><fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long"/></p>
        <p><fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/></p>
        <p><fmt:formatDate value="${now}" type="both" pattern="yyyy년 MM월 dd일 HH시 mm분 ss초 E요일"/></p>
    </body>
</html>
결과를 표시하면 아래와 같다.
JSTL fmt formatDate
2018. 7. 6
오전 12:19:48
2018. 7. 6 오전 12:19:48
2018. 7. 6 오전 12:19:48
18. 7. 6 오전 12:19
2018. 7. 6 오전 12:19:48
2018년 7월 6일 (금) 오전 12시 19분 48초
2018년 7월 6일 금요일 오전 12시 19분 48초 KST
2018년 07월 06일 00시 19분 48초 금요일