728x90

-개발환경-

IDE : Eclipse IDE for Enterprise Java Developers ver-2020-06 (4.16.0)
Tomcat : Tomcat v8.0 Server
Java : java 8

 

-calendar.jsp-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<%@page import="com.koreait.myCalendar.LunarDate"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.koreait.myCalendar.SolarToLunar"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.koreait.myCalendar.MyCalendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>만년달력</title>
<link rel="stylesheet" href="calendar.css">
</head>
 
<body>
<%
Calendar calendar = Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DAY_OF_MONTH);
 
    try{
    year=Integer.valueOf(request.getParameter("year"));
    month=Integer.valueOf(request.getParameter("month"));
    }catch(Exception e){
    }
    if(month<1){
        month=12;
        year--;
    }else if(month>12){
        month=1;
        year++;
    }
    
    ArrayList<LunarDate> lunarDate=SolarToLunar.solarToLunar(year, month);
    
%>
 
<!-- 달력제작 -->
<table width="700" align="center" border="1" cellpadding="5" cellspacing="1">
    <tr>
        <th>
            <input type="button" value="이전달" onclick="location.href='?year=<%=year%>&month=<%=month-1%>'"/>
        </th>
        <th id="title" colspan="5">
            <%=String.format("%4d",year)%>년 <%=String.format("%02d",month)%>
        </th>
        <th>
            <%-- <a href="?year=<%=year%>&month=<%=month+1%>">다음달</a> --%>
            <input type="button" value="다음달" onclick="location.href='?year=<%=year%>&month=<%=month+1%>'"/>
        </th>
    </tr>
    <tr>
        <td id="sunday"></td>
        <td class="etcday"></td>
        <td class="etcday"></td>
        <td class="etcday"></td>
        <td class="etcday"></td>
        <td class="etcday"></td>
        <td id="saturday"></td> <!--단위는 px(메인),em 등..   -->
    </tr>
    
    <!-- 달력에 날짜를 출력한다. -->
    <tr>
<%    
 
    //이전달 일자 출력
        int lastYear=year;
        int lastMonth=month-1;
        if(lastMonth<1){
    lastYear--; 
    lastMonth=12;
        }
        int endDay=MyCalendar.lastDay(lastYear, lastMonth);
        int numDay=MyCalendar.weekDay(year, month, 1);
    for(int i=0; i<MyCalendar.weekDay(year, month, 1);i++){
        int newDay=endDay-numDay+i+1;
        if(i==0)
    out.println("<td class=\"beforesun\">"+lastMonth+"/"+newDay+"</td>");
        else
    out.println("<td class=\"before\" >"+lastMonth+"/"+newDay+"</td>");
    }
    
    //현재달 일자 출력
    int lastday=MyCalendar.lastDay(year, month);
    for(int i=1;i<=lastday;i++){        
        
        if(lunarDate.get(i-1).getLunar().length()==0){
    
            if (MyCalendar.weekDay(year, month, i) == 0)
                out.println("<td class=\"sun\">" + i + "</td>");
            else if (MyCalendar.weekDay(year, month, i) == 6)
                out.println("<td class=\"sat\">" + i + "</td>");
            else
                out.println("<td class=\"etc\">" + i + "</td>");
        }else{
            out.println("<td class='holiday'>"+i+lunarDate.get(i-1).getLunar()+"</td>");
        }
        
        if (MyCalendar.weekDay(year, month, i) == 6 && i + 1 <= lastday) {
            out.println("</tr><tr>");
        }
    }
 
    //다음달 일자 출력
    int nextYear = year;
    int nextMonth = month + 1;
    if (nextMonth > 12) {
        nextYear++;
        nextMonth = 1;
    }
    int DayofWeek = MyCalendar.weekDay(year, month, MyCalendar.lastDay(year, month));
    int start = 0;
    for (int i = DayofWeek + 1; i <= 6; i++) {
        if (i == 6)
    out.println("<td class='aftersat'>" + nextMonth + "/" + ++start + "</td>");
        else
    out.println("<td class='after' >" + nextMonth + "/" + ++start + "</td>");
    }
%>
    </tr>
    
    <tr>
        <td colspan="7">
            <form action="?" method="post">
                <select class="select" name="year">
<%
    for(int i=1950; i<=2050; i++){
        if(i== calendar.get(Calendar.YEAR)){
            out.println("<option selected='selected'>"+i+"</option>");
        }
        else
            out.println("<option>"+i+"</option>");
    }
%>                    
                </select>
                <select class="select" name="month">
<%
    for(int i=1; i<=12; i++){
        if(i== calendar.get(Calendar.MONTH)+1){
            out.println("<option selected='selected'>"+i+"</option>");
        }
        else
            out.println("<option>"+i+"</option>");
    }
%>                    
                </select>
                <input class="select" type="submit" value="보기" />
            </form>
        </td>
    </tr>
    
</table>
</body>
</html>
cs
더보기

-Description

13 : css 파일 지정

24~25 : 리로딩 전 페이지로부터 데이터를 받아온다.
28~34, 49~52 : 이전달,다음달 버튼 클릭시 month가 변경됨에 따라 year도 변경
36 : ArrayList형 lunarDate에 크롤링에서 받아온 양력,음력 공휴일데이터 가져오기.
44 : onclick="location.href='?year=<%=year%>&month=<%=month-1%>'"
onclick을 통해 현재페이지(?)로 리로드,  동시에 year와 month로 데이터 전달
69~83 : 이전달 일자 출력

91: i 날짜가 일요일일 경우엔 sun class의 css파일 적용
93: i 날짜가 토요일일 경우엔 sat class의 css파일 적용
91: i 날짜가 일,토요일이외인 경우엔 etc class의 css파일 적용
98 : i날짜가 공휴일인경우엔 holiday class의 css파일을 날짜와 공휴일명에 적용
101~102 : 출력한 날짜가 토요일이면 줄을 변경, </tr><tr>이런 테크닉에도 유의
86~104 : 현재달 일자 출력
107~120 : 다음달 일자 출력
124~152 : 년,월을 선택하고 보기 버튼을 클릭하면 특정 달의 달력으로 넘어가게 한다
127 : select tag로 년도 선택가능한 combobox 제작
131 : select tag의 기본선택으로 현재년도 지정,
select tag의 하위 태그인 option에서  selected='selected' 을 이용하여 기본선택지정
140~146 : 같은 원리로 select tag의 기본선택으로 현재월을 지정
149 : form tag의 action과 연결 ,  두가지 콤보박스의 선택된 데이터가 넘어감

 

-MyCalendar.java-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.koreait.myCalendar;
 
//달력작업에 필요한 메소드를 모아놓은 클래스
public class MyCalendar {
    
    public static boolean isYun(int year) {
        if (year%4==0 && year%100!=0 || year%400==0)
                return true;
        else
            return false;
    }
    
    public static int lastDay(int year,int month) {
        int[] m = {31,28,31,30,31,30,31,31,30,31,30,31};
        m[1=  isYun(year) ? 29 :28;
        return m[month-1];
    }
    
    public static int totalDay(int year,int month,int day) {
        int total= (year-1)*365  + (year-1)/4- (year-1/ 100 + (year-1)/400;
        for(int i=1; i<month;i++)
            total+=lastDay(year,i);
        return total+day;
        
    }
    
    public static int weekDay(int year,int month,int day) {
        return totalDay(year,month,day) % 7;
    }
}
 
cs
더보기

-Description

6 : year을 인수로 , 윤년, 평년을 판단해 윤년은 true, 평년은 false를 리턴하는 메소드
13 : year,month을 인수로  ,  그 달의 마지막 날짜를 리턴하는 메소드
19 : year,month,day를 인수로 ,  1년 1월 1일 부터 지나온 날짜의 합계를 리턴하는 메소드
27 : year,month,day를 인수로 , 요일을 숫자로 리턴하는 메소드,일요일(0) , 월요일(1),....., 토요일(6)

-LunarDate.java-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.koreait.myCalendar;
 
// 양력 날짜, 양력 날짜에 해당하는 음력날짜, 공휴일을 묶음으로 기억하는 클래스
public class LunarDate {
    private int year;  //양력년
    private int month; //양력월
    private int day;  //양력일
    private int yearLunar;  //음력년
    private int monthLunar; //음력월
    private int dayLunar;  //음력일
    private boolean LunarFlag; //true면 윤달
    private String Lunar="";//공휴일
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public int getYearLunar() {
        return yearLunar;
    }
    public void setYearLunar(int yearLunar) {
        this.yearLunar = yearLunar;
    }
    public int getMonthLunar() {
        return monthLunar;
    }
    public void setMonthLunar(int monthLunar) {
        this.monthLunar = monthLunar;
    }
    public int getDayLunar() {
        return dayLunar;
    }
    public void setDayLunar(int dayLunar) {
        this.dayLunar = dayLunar;
    }
    public boolean isLunarFlag() {
        return LunarFlag;
    }
    public void setLunarFlag(boolean lunarFlag) {
        LunarFlag = lunarFlag;
    }
    public String getLunar() {
        return Lunar;
    }
    public void setLunar(String lunar) {
        Lunar = lunar;
    }
    @Override
    public String toString() {
        return String.format("앙력 %4d년 %2d월 %2d일은 음력 %s %4d년 %2d월 %2d일 입니다 . -%s",
                year,month,day,LunarFlag?"윤":"",yearLunar,monthLunar,dayLunar,Lunar);
    }
    
    
    
}
 
cs
더보기

-Description

양력, 음력, 공휴일데이터를 저장하는 데이터 클래스

-SolarToLunar.java-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.koreait.myCalendar;
 
import java.io.IOException;
import java.util.ArrayList;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
public class SolarToLunar {
 
    public static ArrayList<LunarDate> solarToLunar(int year,int month){
        
        ArrayList<LunarDate> lunarList = new ArrayList<LunarDate>();//1월~12월의 양력과 대응 되는 음력을 기억.
        ArrayList<LunarDate> list = new ArrayList<LunarDate>();//음력을 저장해 리턴할 객체
        String targetSite="";
        
        
            targetSite=String.format("https://astro.kasi.re.kr/life/pageView/5?search_year=%4d&search_month=%02d",year,month);
            
            Document document=null;
            
        try {
            document = Jsoup.connect(targetSite).get(); 
            //select()메소드를 사용해 날짜<tr>단위로 얻어온다.
            Elements elements = document.select("tbody > tr");
            for(Element element : elements) {
                Elements ele = element.select("td");
//                System.out.println(ele.get(0));//양력
//                System.out.println(ele.get(1));//음력
                String sola=ele.get(0).text();//양력
                String lunar=ele.get(1).text();//음력
                
                LunarDate lunarDate = new LunarDate();
                lunarDate.setYear(Integer.valueOf(sola.split("-")[0]));
                lunarDate.setMonth(Integer.valueOf(sola.split("-")[1]));
                lunarDate.setDay(Integer.valueOf(sola.split("-")[2].split(" ")[0]));
                
                lunarDate.setYearLunar(Integer.valueOf(lunar.split("-")[0]));
                lunarDate.setMonthLunar(Integer.valueOf(lunar.split("-")[1]));
                lunarDate.setDayLunar(Integer.valueOf(lunar.split("-")[2]));
            
                lunarDate.setLunarFlag(lunar.length()>10?true:false);
                
                lunarList.add(lunarDate);
            }
        } 
        catch (IOException e) { e.printStackTrace();     }
        
        //반복문 끝
 
        //공휴일처리
        for(int i=0;i<lunarList.size();i++) {
            
            //양력 공휴일
            if(lunarList.get(i).getMonth()==1 && lunarList.get(i).getDay()==1
                lunarList.get(i).setLunar("</br><span>신정</span>");
            else if(lunarList.get(i).getMonth()==3 && lunarList.get(i).getDay()==1
                lunarList.get(i).setLunar("</br><span>삼일절</span>");
            else if(lunarList.get(i).getMonth()==5 && lunarList.get(i).getDay()==1
                lunarList.get(i).setLunar("</br><span>근로자의날</span>");
            else if(lunarList.get(i).getMonth()==5 && lunarList.get(i).getDay()==5
                lunarList.get(i).setLunar("</br><span>어린이날</span>");
            else if(lunarList.get(i).getMonth()==6 && lunarList.get(i).getDay()==6
                lunarList.get(i).setLunar("</br><span>현충일</span>");
            else if(lunarList.get(i).getMonth()==8 && lunarList.get(i).getDay()==15
                lunarList.get(i).setLunar("</br><span>광복절</span>");
            else if(lunarList.get(i).getMonth()==10 && lunarList.get(i).getDay()==3
                lunarList.get(i).setLunar("</br><span>개천절</span>");
            else if(lunarList.get(i).getMonth()==10 && lunarList.get(i).getDay()==9
                lunarList.get(i).setLunar("</br><span>한글날</span>");
            else if(lunarList.get(i).getMonth()==12 && lunarList.get(i).getDay()==25
                lunarList.get(i).setLunar("</br><span>크리스마스</span>");
            
            //음력 공휴일 
            if(!lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==1 && lunarList.get(i).getDayLunar()==1) {
                lunarList.get(i-1).setLunar("</br><span>설날연휴</span>");
                lunarList.get(i).setLunar("</br><span>설날</span>");
                lunarList.get(i+1).setLunar("</br><span>설날연휴</span>");
            }
            else if(!lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==4 && lunarList.get(i).getDayLunar()==8
                lunarList.get(i).setLunar("</br><span>석가탄신일</span>");
            else if(!lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==8 && lunarList.get(i).getDayLunar()==15) {
                lunarList.get(i-1).setLunar("</br><span>추석연휴</span>");
                lunarList.get(i).setLunar("</br><span>추석</span>");
                lunarList.get(i+1).setLunar("</br><span>추석연휴</span>");
            }
            
            //대체 공휴일 => 설, 추석, 어린이날
            if(lunarList.get(i).getMonth()==5 && (lunarList.get(i).getDay()==6 || lunarList.get(i).getDay()==7&& MyCalendar.weekDay(year,month,i)==1
                lunarList.get(i).setLunar("</br><span>대체 공휴일</span>");
            else if(!lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==1 &&
                    (lunarList.get(i).getDayLunar()==2 || lunarList.get(i).getDayLunar()==3&& MyCalendar.weekDay(year,month,i)==1
                lunarList.get(i).setLunar("</br><span>대체공휴일</span>");
            else if(!lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==8 &&
                    (lunarList.get(i).getDayLunar()==16 || lunarList.get(i).getDayLunar()==17&& MyCalendar.weekDay(year,month,i)==1
                lunarList.get(i).setLunar("</br><span>대체공휴일</span>"); 
            
            //어린이날 대체 공휴일 => 어린이날이 석가탄신일과 겹쳤을때
            if(lunarList.get(i).getMonth()==5 && lunarList.get(i).getDay()==5 
                    && !lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==4 && lunarList.get(i).getDayLunar()==8) {
                lunarList.get(i).setLunar("</br><span>어린이날</span></br><span>석가탄신일</span>");
                lunarList.get(i+1).setLunar("</br><span>대체 공휴일</span>");
            }
            
            //추석 대체 공휴일 => 추석이 개천절과 겹쳤을때
            if(lunarList.get(i).getMonth()==10     && lunarList.get(i).getDay()==3 
                    && !lunarList.get(i).isLunarFlag() && lunarList.get(i).getMonthLunar()==8 && lunarList.get(i).getDayLunar()==15) {
                lunarList.get(i).setLunar("</br><span>추석</span></br><span>개천절</span>");
                lunarList.get(i+2).setLunar("</br><span>대체 공휴일</span>");
            }
        }
 
 
        
        for(int i=0; i< lunarList.size();i++
            if(lunarList.get(i).getMonth()== month) 
                list.add(lunarList.get(i));
 
        return list;
    }
    
}
 
cs
더보기

-Description

13 : 월별 양력과 음력을 크롤링하고 양력, 음력 공휴일을 계산해서 리턴하는 메소드
22 : 크롤링한 데이터를 기억할 org.jsoup.nodes 패키지의 Document 클래스 객체를 선언한다.
25 : org.jsoup.Jsoup 패키지의 connect() 메소드로 크롤링 할 타겟 사이트에 접속하고 
      get() 메소드로 타겟사이트의 정보를 얻어온다.
27 : tbody > tr > td 내부에 양력 음력 정보가 들어 있으므로 , 일단 tbody > tr 을 select함

 

32~33 : text() 메소드로 태그제외한 텍스트만 가져옴, 즉 양력 음력 데이터 
35~44:  크롤링한 결과를 반복문 마다 LunarDate 클래스 객체에 저장
44줄의 lunarDate.setLunarFlag에는 항상 false가 인수로 넘어감
46 : lunarList에는 특정년도에 해당하는 모든달 모든일에 대한 정보가 들어감.
54 : lunarList에 있는 정보들을 하나씩 뽑아와서 공휴일(Lunar)명칭을 지정
57~74 : 양력 공휴일 지정
77~88 : 음력 공휴일 지정 
91~98 : 대체 공휴일 지정 ( 토 or 일요일에 설,추석,어린이날이 있다면  , 월요일을 대체 공휴일로 지정 )
117~121 : lunarList 데이터를 list로 복사후 반환.

-calendar.css-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
table{
    /* border-xxx: 선두께 선종류 선색상; */
    /* border-left: 0px solid; */ 
    /* border-radius: 선의 굴곡; */ 
    border: 3px solid navy;
    border-radius: 10px;
    
}
tr {
    height: 80px;
    border: 0px solid;
}
th{
    border: 0px solid;
}
td{
    border: 1px solid;
    border-radius: 5px;
}
/* .은 클래스 명명 */ 
.select{
    width: 100px;
    height: 30px;
}
/* #은 id 명명 */
th#title {
    font-size: 30pt;
    color: tomato;
    font-weight: bold;
    font-family: D2Coding;
}
td#sunday {
    font-size: 20pt;
    font-weight: bold;
    width: 100px;
    text-align: center;
    color: red;
}
 
td#saturday {
    font-size: 20pt;
    font-weight: bold;
    width: 100px;
    text-align: center;
    color: blue;
}
td.etcday {
    font-size: 20pt;
    font-weight: bold;
    width: 100px;
    text-align: center;
    color: black;
}
td.sun{
    color: red;
    font-weight: bold;
    text-align:  right;
    vertical-align: top;
}
td.sat{
    color: blue;
    font-weight: bold;
    text-align:  right;
    vertical-align: top;
}
td.etc{
    color: black;
    font-weight: bold;
    text-align:  right;
    vertical-align: top;
}
td.beforesun{
    color: red;
    font-weight: bold;
    font-size: 5px;
    text-align:  right;
    vertical-align: top;
    background: lightgray;
}
td.before{
    color: black;
    font-weight: bold;
    font-size: 5px;
    text-align:  right;
    vertical-align: top;
    background: lightgray;
}
td.aftersat{
    color: blue;
    font-weight: bold;
    font-size: 5px;
    text-align:  right;
    vertical-align: top;
    background: lightgray;
}
td.after{
    color: black;
    font-weight: bold;
    font-size: 5px;
    text-align:  right;
    vertical-align: top;
    background: lightgray;
}
td.holiday{
    color: red;
    font-weight: bold;
    font-size: 5px;
    text-align:  right;
    vertical-align: top;
    font-size: 12pt;
    background: yellow;
}
span{
    font-size: 9pt;
}
/*하이퍼 링크 스타일 지정하기*/
/* a 는 일괄 하이퍼 링크 특성 지정*/
/* a {
    color: black;
    text-decoration: none;
} */
a:link { /* link의 의미는 , 한 번도 방문하지 않은 하이퍼 링크*/
    color: black;
    text-decoration:none;
}
a:visited { /* 한 번 이상 클릭한 하이퍼 링크*/
    color: black;
    text-decoration:none;
}
a:hover{ /*마우스를 올리고 있을 때 발생함*/
    text-decoration:underline;
}
a:active{ /*마우스로 누르고 있을 때 발생함*/
    color: skyblue;
    text-decoration:none;
}
 
 
cs
더보기

-Description

정통적으로 ,
.=>class (클래스는 여러번나올때 사용) 
 # => id 를 의미 (id는 하나씩 지정할때 사용) 

 

-요약-

 

-마침글-

 

'21년이전 > 국비-JSP' 카테고리의 다른 글

국비-쿠키(Cookie)  (0) 2021.03.19
JSP-출석 게시판 구현  (0) 2021.03.18
JSP-인클루드 액션태그 이용  (0) 2021.03.14
JSP-코로나 실시간 온라인 투표  (0) 2021.03.04
JSP-html 구조  (0) 2021.02.25

+ Recent posts