본문 바로가기

Study/Programming

JSP 달력

반응형
calendar.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calendar</title>
</head>
<body>
<p>

<form action="cal_ok.jsp" method="post">
<select name="year">
    <option value="2007">2007년</option>
    <option value="2008">2008년</option>
    <option value="2009">2009년</option>
    <option value="2010">2010년</option>
</select>
<select name="month">
    <option value="1">1월</option>
    <option value="2">2월</option>
    <option value="3">3월</option>
    <option value="4">4월</option>
    <option value="5">5월</option>
    <option value="6">6월</option>
</select>
<input type="submit" value="전송하기"/>
</form>

</body>
</html>


cal_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.Calendar"%>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");

    String sYear = request.getParameter("year");
    String sMonth = request.getParameter("month");
   
    Calendar sDay = Calendar.getInstance();
    Calendar eDay = Calendar.getInstance();
    Calendar cDay = Calendar.getInstance();

    StringBuffer sb = new StringBuffer();

    int iYear = Integer.parseInt(sYear);
    int iMonth = Integer.parseInt(sMonth);
   
    sDay.set(iYear, iMonth-1, 1);
    eDay.set(iYear, iMonth, 0);
   
    sb.append("현재 날짜 : " +cDay.get(Calendar.DATE) +"<br>");
    sb.append("시작 날짜 : " +sDay.get(Calendar.DATE) +"<br>");
    sb.append("마지막 날짜 : " +eDay.get(Calendar.DATE) +"<br>");
    sb.append("시작 요일 :" +sDay.get(Calendar.DAY_OF_WEEK) +"<p>");

    sb.append("     " +iYear +"년 " +iMonth +"월"+"<br>");
    sb.append("<table border='0'>");
    sb.append("<tr>");
    sb.append("<td>" +"일" +"</td>");
    sb.append("<td>" +"월" +"</td>");
    sb.append("<td>" +"화" +"</td>");
    sb.append("<td>" +"수" +"</td>");
    sb.append("<td>" +"목" +"</td>");
    sb.append("<td>" +"금" +"</td>");
    sb.append("<td>" +"토" +"</td>");
    sb.append("</tr>");
    sb.append("<tr>");
   
    for(int i=1; i<sDay.get(Calendar.DAY_OF_WEEK); i++){
        sb.append("<td>");
        sb.append("");
        sb.append("</td>");
    }

    for(int i=sDay.get(Calendar.DATE), n=sDay.get(Calendar.DAY_OF_WEEK); i<=eDay.get(Calendar.DATE); i++, n++){
        sb.append("<td>");
       
        sb.append((i<10)?" 0"+i : " "+i);
//        if(i == cDay.get(Calendar.DATE)){
//            sb.append("<td bgcolor='yellow'>" +i +"</td>");        
//        }
        if(n%7==0){
            sb.append("</td></tr>");
        }
    }

    sb.append("</table>");
%>
<%= sb %>
</body>
</html>



반응형

'Study > Programming' 카테고리의 다른 글

JSP 간단한 게시판  (0) 2009.05.29
JSP Encoder.Decoder  (0) 2009.05.29
JSP get/post 값 넘겨주기  (0) 2009.05.28
JSP submit get/post전송  (0) 2009.05.28
JSP 서버이름, 클라이언트ip  (0) 2009.05.28