Java

음력읽기

top2blue 2017. 7. 4. 18:20
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
package kr.baeoom.util;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
public class LunarRead {
    public static void main(String[] args) {
        List<Lunar> cal201706 = getLunar(20177);
        System.out.println(cal201706.size() + "개");
        for(Lunar lunar : cal201706){
            System.out.println(lunar.getGangiYearH() + "-" + 
                               lunar.getGangiMonthH() + "-" + 
                               lunar.getGangiDateK() + " : " 
                               + lunar.getSolarWeek() + " : " 
                               + lunar.isLeapMonth());
        }
    }
    // 달력 데이터를 1개월치를 읽어서 리턴하는 메서드 
    public static List<Lunar> getLunar(int year,int month) {
        List<Lunar> list = new ArrayList<>();
        Document doc = null;
        String addr = "https://astro.kasi.re.kr:444/life/pageView/5";
        try {
            addr += "?search_year="+ year + "&search_month="+String.format("%02d",month);
            doc = Jsoup.connect(addr).get(); // 연결하여 Document객체를 얻는다.
            // 원하는 부분만 가져온다.
            // div태그의 클래스이름이 "table-responsive"인것만 찾아서 가져온다.
            Elements elements = doc.select("div.table-responsive");
            // Elements captions = elements.select("caption"); // 그중에서 caption태그 내용만 
            // System.out.println(captions.text());
            Elements trs = elements.select("tbody tr");
            for(Element e : trs){
                Elements tds = e.select("td");
                Lunar lunar = new Lunar();
                lunar.setSolar(tds.get(0).text());
                lunar.setLunar(tds.get(1).text());
                lunar.setGangi(tds.get(2).text());
                list.add(lunar);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
 
cs