-
JSP 기본적인 달력 예시Web Programming 2023. 2. 17. 17:33728x90반응형
<%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page import="java.util.HashMap" %> <%@ page import="java.util.LinkedHashMap" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.Calendar" %> <%@ page import="java.util.Locale" %> <%@ page import="java.util.Map"%> <%@ page import="com.ibm.icu.util.ChineseCalendar"%> <%@ page import="java.util.Arrays"%> <%@ page import="java.text.DecimalFormat"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <% request.setCharacterEncoding("UTF-8"); String contextPath = request.getContextPath(); String listPage = "/goAllList"; Map<String,Object> map = null; // 달력 Calendar cal = Calendar.getInstance(); SimpleDateFormat smdf = new SimpleDateFormat("yyyy-MM-dd"); String strToday = smdf.format(cal.getTime()); String year = Integer.toString(cal.get(Calendar.YEAR)); String month = cal.get(Calendar.MONTH)+1<10?"0" + Integer.toString(cal.get(Calendar.MONTH)+1):Integer.toString(cal.get(Calendar.MONTH)+1); String day = cal.get(Calendar.DATE)<10?"0" + Integer.toString(cal.get(Calendar.DATE)):Integer.toString(cal.get(Calendar.DATE)); String calDayStr = ""; int currentYear = Integer.parseInt(year); int currentMonth = Integer.parseInt(month) - 1; cal = Calendar.getInstance(); cal.set(currentYear, currentMonth, 1); int startNum = cal.get(Calendar.DAY_OF_WEEK); //선택 월의 시작요일을 구한다. int lastNum = cal.getActualMaximum(Calendar.DATE); // 선택 월의 마지막 날짜를 구한다. (2월인경우 28 또는 29일, 나머지는 30일과 31일) cal.set(Calendar.DATE, lastNum); // Calendar 객체의 날짜를 마지막 날짜로 변경한다. int weekNum = cal.get(Calendar.WEEK_OF_MONTH); // 마지막 날짜가 속한 주가 선택 월의 몇째 주인지 확인한다. 이렇게 하면 선택 월에 주가 몇번 있는지 확인할 수 있다. int calCnt = weekNum * 7; // 반복횟수를 정한다 int dateCnt = 1; cal = Calendar.getInstance(); cal.set(Calendar.YEAR, Integer.parseInt(year)); cal.set(Calendar.MONTH, Integer.parseInt(month)-1); cal.set(Calendar.DATE, Integer.parseInt(day)); cal.add(Calendar.MONTH, -1); String preMonth = smdf.format(cal.getTime()); cal.add(Calendar.MONTH, 2); String nextMonth = smdf.format(cal.getTime()); String eDay = ""; String sDay = "01"; if("01".equals(preMonth.split("-")[1]) || "03".equals(preMonth.split("-")[1]) || "05".equals(preMonth.split("-")[1]) || "07".equals(preMonth.split("-")[1]) || "08".equals(preMonth.split("-")[1]) || "10".equals(preMonth.split("-")[1]) || "12".equals(preMonth.split("-")[1])){ eDay = "31"; } if("04".equals(preMonth.split("-")[1]) || "06".equals(preMonth.split("-")[1]) || "09".equals(preMonth.split("-")[1]) || "11".equals(preMonth.split("-")[1])){ eDay = "30"; } if("02".equals(preMonth.split("-")[1])){ if("2040".equals(preMonth.split("-")[0]) || "2036".equals(preMonth.split("-")[0]) || "2032".equals(preMonth.split("-")[0]) || "2028".equals(preMonth.split("-")[0]) || "2024".equals(preMonth.split("-")[0]) || "2020".equals(preMonth.split("-")[0]) || "2004".equals(preMonth.split("-")[0])){ eDay = "29"; }else{ eDay = "28"; } } cal = Calendar.getInstance(); cal.set(Calendar.YEAR, Integer.parseInt(year)); cal.set(Calendar.MONTH, Integer.parseInt(month)-1); cal.set(Calendar.DATE, Integer.parseInt(day)); //달력 %> <div class="calUtil"> <strong><%=year%>.<em><%=month%></em></strong> <% %> <% if(preMonth.split("-")[0].equals("2022") && preMonth.split("-")[1].equals("06")) { %> <% } else { %> <button type="button" class="mPrev" onclick="location.href='<%=listPage%>&year=<%=preMonth.split("-")[0]%>&month=<%=preMonth.split("-")[1]%>&day=<%=eDay%>&focus=on' "> <%if("0".equals(preMonth.substring(5,6))){%> <%=preMonth.substring(6,7)%>월 <%}else{%> <%=preMonth.substring(5,7)%>월 <%}%> <% } %> </button> <button type="button" class="mNext" onclick="location.href='<%=listPage%>&year=<%=nextMonth.split("-")[0]%>&month=<%=nextMonth.split("-")[1]%>&day=<%=sDay%>&focus=on'"> <%if("0".equals(nextMonth.substring(5,6))){%> <%=nextMonth.substring(6,7)%>월 <%}else{%> <%=nextMonth.substring(5,7)%>월 <%}%> </button> </div> <table> <caption>월/요일별 일정</caption> <colgroup> <col style="width:14.2%"> <col style="width:14.2%"> <col style="width:14.2%"> <col style="width:14.2%"> <col style="width:14.2%"> <col style="width:14.2%"> <col> </colgroup> <thead> <tr> <th scope="col" class="sun">일</th> <th scope="col" id="first">월</th> <th scope="col">화</th> <th scope="col">수</th> <th scope="col">목</th> <th scope="col">금</th> <th scope="col" class="sat">토</th> </tr> </thead> <tbody> <% String classStr = ""; for(int i=0; i<weekNum; i++){ %> <tr> <% for(int j=0; j<7; j++){ if(j==0){ classStr += "sun"; }else if(j==6){ classStr += "sat"; } if (startNum - 1 > 0 || dateCnt > lastNum) { calDayStr = ""; startNum--; %> <td></td> <% } else { cal = Calendar.getInstance(); calDayStr = year +"-" + (Integer.parseInt(month)<10&&month.length()<2?"0"+month:month) + "-" + (dateCnt<10?"0"+Integer.toString(dateCnt):Integer.toString(dateCnt)); if(!"".equals(getString(map, calDayStr))){ classStr += " on2"; } if(calDayStr.equals(smdf.format(cal.getTime()))){ classStr += ""; } String title = ""; %> <%if("0".equals(calDayStr.substring(8,9))){%> <%if(strToday.equals(calDayStr)){%> <td class="calTd <%=classStr%>" id="<%=calDayStr%>"> <%if(strToday.equals(year+"-"+month+"-"+day)){%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" class="today" id="<%=calDayStr%>a"> <%=calDayStr.substring(9,10)%> </a> <%}else{%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" id="<%=calDayStr%>a"> <%=calDayStr.substring(9,10)%> </a> <%}%> </td> <%}else{%> <td class="calTd <%=classStr%>" id="<%=calDayStr%>"> <%if(calDayStr.equals(year+"-"+month+"-"+day)){%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" class="today" id="<%=calDayStr%>a"> <%=calDayStr.substring(9,10)%> </a> <%}else{%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" id="<%=calDayStr%>a"> <%=calDayStr.substring(9,10)%> </a> <%}%> </td> <%}%> <%}else{%> <%if(strToday.equals(calDayStr)){%> <td class="calTd <%=classStr%>" id="<%=calDayStr%>"> <%if(strToday.equals(year+"-"+month+"-"+day)){%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" class="today" id="<%=calDayStr%>a"> <%=calDayStr.substring(8,10)%> </a> <%}else{%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" id="<%=calDayStr%>a"> <%=calDayStr.substring(8,10)%> </a> <%}%> </td> <%}else{%> <td class="calTd <%=classStr%>" id="<%=calDayStr%>"> <%if(calDayStr.equals(year+"-"+month+"-"+day)){%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" class="today" id="<%=calDayStr%>a"> <%=calDayStr.substring(8,10)%> </a> <%}else{%> <a href="javascript:selectToday('<%=calDayStr.split("-")[0]%>','<%=calDayStr.split("-")[1]%>','<%=calDayStr.split("-")[2]%>');" id="<%=calDayStr%>a"> <%=calDayStr.substring(8,10)%> </a> <%}%> </td> <%}%> <%}%> <% dateCnt++; } classStr = ""; } %> </tr> <% } %> </tbody> </table> <%! public String getString(Map<String,Object> map, String key){ String val = ""; if(map!=null && map.get(key)!=null){ val = map.get(key).toString(); } return val; } %>
결과
다음달을 보기 위해 1월, 3월 클릭 시 호출되는 url 은 아래와 같다
/goAllList&year=2023&month=03&day=01&focus=on
이 부분에서
String year = Integer.toString(cal.get(Calendar.YEAR)); String month = cal.get(Calendar.MONTH)+1<10?"0" + Integer.toString(cal.get(Calendar.MONTH)+1):Integer.toString(cal.get(Calendar.MONTH)+1); String day = cal.get(Calendar.DATE)<10?"0" + Integer.toString(cal.get(Calendar.DATE)):Integer.toString(cal.get(Calendar.DATE));
받은 파라미터를 바꿔주면 됨
String year = request.getParameter("year"); String month = request.getParameter("month"); String day = request.getParameter("day");
728x90반응형'Web Programming' 카테고리의 다른 글
UNION 과 UNION ALL 의 차이 (0) 2023.02.17 쿼리 Count(1)의 의미 (0) 2023.02.17 티베로 DB 버전 확인 (0) 2023.02.17 크롤링(Crawling) (0) 2023.02.17 CI , DI 차이와 문제점 (0) 2023.02.17