티스토리 뷰

JAVA의 Calendar, SimpleDateFormat 라이브러리를 이용하여 현재 날짜와 시간을 JSP에서 다룰 수 있다. 

방법은 매우 간단하다.


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.util.Calendar" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<%
	Calendar date = Calendar.getInstance(); //날짜와 시간을 전부 가지고 있음, Calendar는 new로 인스턴스를 생성하지 않음
	SimpleDateFormat today = new SimpleDateFormat("yyyy년 MM월 dd일"); // 날짜 형식을 생성	
	SimpleDateFormat now = new SimpleDateFormat("hh시 mm분 ss초");  // 시간 형식 생성	
%>
<!DOCTYPE>
<html>
<head>
	<meta charset="UTF-8">
	<title>외부 라이브러리 사용 : 날짜 시간</title>
</head>
<body>
	<h5> 외부 라이브러리 사용 : 날짜 시간 </h5>
	오늘은 <b> <%= today.format(date.getTime()) %></b> 이고, <br />
	지금 시간은 <b> <%= now.format(date.getTime()) %></b> 입니다.
	<!--  date.getTime()은 날짜와 현재 시간을 반환하는 메서드 -->
	
</body>
</html>

Calendar 의 인스턴스를 그대로 받아 사용할 수도 있지만, 형식이 읽기 편한 형태로 나오지는 않기 때문에, 위와 같이 SimpleDateFormat을 사용하여 년월일 형태로 나타낸다.

 

 

 

Comments