JSTL Formmating Library의 개요와 사용법에 대해 알아보자
JSTL 분류
2️⃣ JSTL Formatting Library
숫자, 날짜 및 시간 데이터의 출력 형식을 지정할 때 사용하는 문법을 제공
💻 jstl.jsp
<a href="02_fomatting.jsp">formatting</a>
02_formatting.jsp 생성
![](https://blog.kakaocdn.net/dn/kJ2NZ/btrQOnq9AyT/pDVHoIZrs7xDw2eOWEFVhK/img.png)
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
1. formatNumber
✔️ 숫자 데이터의 포맷(형식) 지정
✔️ 표현하고자 하는 숫자 데이터의 형식을 통화 기호, % 등의 원하는 쓰임에맞게 지정하는 태그
<ftm;formatNumber value="출력할값" groupingUsed="true/false" type="percent/currencySymbol="$">
👉🏻 groupingUsed, type, currncySymbol 속성은 생략 가능
👉🏻 groupingUsed : 숫자 단위의 구분자 (,) 표시 여부를 지정하는 속성(생략 가능, 생략 시 기본값이 true => 구분자를 찍겠다)
👉🏻 type: "percent" (백분율 단위로 보여지겠다) / "currency" (돈 단위로 보여지겠다 => 로컬 컴퓨터 기준)
👉🏻 currencySymbol : type 속성이 "currency"일 경우 통화 기호 문자의 종류를 지정
💻 02_formatting.jsp - 테스트 공통 내용
<head>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
</head>
<body>
<!-- 포맷팅 테스트를 위한 변수 먼제 세팅 -->
<c:set var="num1" value="123456789" />
<c:set var="num2" value="0.75" />
<c:set var="num3" value="50000" />
</body>
💻 02_formatting.jsp - test 1(groupingUsed)
그냥 출력: ${ num1 } <br>
formattingNumber 태그를 이용해서 출력: <fmt:formatNumber value="${ num1 }" /> <br>
숫자 그대로 출력: <fmt:formatNumber value="${ num1 }" groupingUsed="false" /> <br>
👉🏻 3자리마다 , 가 찍혀 있음
👉🏻 groupingUsed 기본값: true(생략 시 기본값) => 3자리마다 , 가 찍혀 있음
✔️ 즉, groupingUsed는 숫자 단위의 구분자 (,) 표시 여부 지정
💻 02_formatting.jsp - test 2 (type="percent")
그냥 출력: ${ num2 } <br>
percent : <fmt:formatNumber value="${ num2 }" type="percent" /> <br>
👉🏻 type: "percent" : 소숫점을 백분율로 변경해서 보여 줌
💻 02_formatting.jsp - test 3 (type="currency")
그냥 출력: ${ num3 } <br>
currency : <fmt:formatNumber value="${ num3 }" type="currency" /> <br>
👉🏻 type="currnecy" : 통화(돈)의 단위로 보여짐
👉🏻 현재 내가 쓰고 있는 컴퓨터의 로컬 정보에 따라서 통화 단위가 달라짐
(groupingUsed 속성의 기본값이 true이기 때문에 3자리마다 , 도 찍혀 있음)
💻 02_formatting.jsp - test 4 (type="currency" currencySymbol="$")
currency $ : <fmt:formatNumber value="${ num3 }" type="currency" currencySymbol="$" /> <br>
👉🏻 currencySymbol : 통화 기호 문자의 종류를 지정
2. formatDate
✔️ 날짜 및 시간 데이터의 포맷(형식) 지정
✔️ 단, java.util.Date 객체를 사용해야 함
type 속성값
👉🏻 date : 날짜를 출력 / 생략 가능하며 생략 시 기본값이 됨
📝 기본 출력 예시: 2022. 11. 10
👉🏻 time : 시간만 출력
📝 기본 출력 예시: 오후 1:52:53
👉🏻both : 시간과 날짜 둘 다 출력
📝 기본 출력 예시: 2022. 11. 10 오후 1:52:53
dateStyle, timeStyle 속성값
👉🏻 medium : 생략 시 기본값
📝 기본 출력 예시: 2022. 11. 10 오후 1:52:53
👉🏻 long :
📝 기본 출력 예시: 2022년 11월 10일 (목) 오후 1시 52분 53초
👉🏻 full :
📝 기본 출력 예시: 2022년 11월 10일 목요일 오후 1시 52분 53초 KST
👉🏻 short :
📝 기본 출력 예시: 22. 11. 10 오후 1:52
✔️ dateStyle, timeStyle 일치시킬 필요 없이 서로 다르게 정의 가능!
pattern 속성
✔️ 커스터마이징 가능!
👉🏻 yyyy: 년도
👉🏻 MM: 월
👉🏻 dd: 일
👉🏻 E: 요일
👉🏻 a: 오전/오후
👉🏻 HH: 시
👉🏻 mm: 분
👉🏻 ss: 초
💻 02_formatting.jsp
<!-- 테스트를 위한 날짜 변수 세팅 -->
<c:set var="current" value="<%= new java.util.Date() %>" />
그냥 출력: ${ current } <br>
<ul>
<li>
현재 날짜: <fmt:formatDate value="${ current }" type="date" />
<!-- type="date" 는 생략 가능! 생략 시 기본값이 "date" : 날짜를 출력 -->
<li>
현재 시간: <fmt:formatDate value="${ current }" type="time" />
<!-- type="time": 시간만 출력 -->
</li>
<li>
현재 날짜 및 시간: <fmt:formatDate value="${ current }" type="both" />
<!-- type="both" : 날짜와 시간 둘 다 출력 -->
</li>
<li>
medium: <fmt:formatDate value="${ current }" type="both" dateStyle="medium" timeStyle="medium" />
<!-- medium: 기본 길이 형식 -->
</li>
<li>
long: <fmt:formatDate value="${ current }" type="both" dateStyle="Long" timeStyle="Long" />
</li>
<li>
full : <fmt:formatDate value="${ current }" type="both" dateStyle="full" timeStyle="full" />
<!-- long보다도 더 긴 형식 -->
</li>
<li>
short : <fmt:formatDate value="${ current }" type="both" dateStyle="short" timeStyle="short" />
</li>
<li>
내 입맛대로 섞어서 지정 : <fmt:formatDate value="${ current }" type="both" dateStyle="full" timeStyle="short" />
<!-- dateStyle 속성값과 timeStyle 속성값을 굳이 일치시킬 필요는 없음! -->
</li>
<li>
customizing : <fmt:formatDate value="${ current }" type="both" pattern="yyyy-MM-dd(E) a HH:mm:ss" />
</li>
</ul>