Java 음력 하지를 어떻게 구하나요 - java eumlyeog hajileul eotteohge guhanayo

자바 예제

[Java] 양력 -> 음력, 음력 -> 양력 변환

자바에서는 기본적으로 날짜 관련된 객체로 calendar 를 이용한다.

하지만 프로그래밍 언어라는게 외국에서 개발되어 우리나라나 중국에서 사용하는 음력 날짜는 지원하지 않는다.

하지만 ibm에서 icu 라는 라이브러리를 만들어 배포하고 있어, 해당 라이브러리에 chianCalendar를 이용하면 음력 날짜를 사용할 수 있다.

사용한 라이브러리 :  ICU4J - International Components for Unicode for Java

사용법 : icu4j.jar 다운로드 받은후 프로젝트의 class path에 추가

 [코드]

Colored By Color Scripter

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
import java.util.Calendar;
import com.ibm.icu.util.ChineseCalendar;


/*
 * icu4j-4.8.1.jar은 중국 음력 기준으로 작성된거라 우리나라 음력과 가끔 틀린 부분이 있다고 합니다.
 * 그부분에 대해서는 직접 수정을 해야 할것 같습니다.
 */


public class LunarCalendar {
    
    private Calendar cal;
    private ChineseCalendar cc;
    
    public LunarCalendar() {
        //default TimeZone, Locale 을 사용
        cal = Calendar.getInstance();
        cc = new ChineseCalendar();        
    }
    
    //테스트를 위한 메인 메소드
    public static void main(String[] args) {
        
        LunarCalendar lc = new LunarCalendar();
        
        System.out.println("2014년 09월 08일에 대한 음력 날짜는?");
        System.out.println("=> "+lc.toLunar("20140908")); //양력을 음력으로 변환
        
        System.out.println("2014년 08월 15일에 대한 양력 날짜는?");
        System.out.println("=> "+lc.fromLunar("20140815")); //음력을 양력으로 변환        
        
    }
    
    
    /**
     * 양력(yyyyMMdd) -> 음력(yyyyMMdd)
     * : 양력을 음력으로 변환 처리한다.
     */

    public synchronized String toLunar(String yyyymmdd) {
        
        if( yyyymmdd == null ) return "";
        
        String date = yyyymmdd.trim();
        
        //
        if( date.length() != 8) { 
            if( date.length() == 4) {
                date = date + "0101";
            }else if(date.length() == 6){
                date = date + "01";                
            }else if(date.length() > 8) {
                date = date.substring(0,8);
            }else {
                return "";
            }
        }
        
        cal.set( Calendar.YEAR,  Integer.parseInt(date.substring(0,4)));
        cal.set( Calendar.MONTH,  Integer.parseInt(date.substring(4,6))-1);
        cal.set( Calendar.DAY_OF_MONTH,  Integer.parseInt(date.substring(6)));
        
        cc.setTimeInMillis(cal.getTimeInMillis());
        
        
        //ChinessCalendar.EXTENDED_YEAR 는 Calendar.YEAR 값과 2637 만큼의 차이를 가짐.
        
        int y = cc.get(ChineseCalendar.EXTENDED_YEAR) - 2637;
        int m = cc.get(ChineseCalendar.MONTH)+1;
        int d = cc.get(ChineseCalendar.DAY_OF_MONTH);
        
        //출력할 문자열을 저장하기 위한 스트링 버퍼 객체
        StringBuffer sb = new StringBuffer();
        
        if( y < 1000 ) sb.append("0");
        else if(y<100) sb.append("00");
        else if(y<10) sb.append("000");
        sb.append(y); //년
        
        if(m < 10 ) sb.append("0");
        sb.append(m); //월
        
        if(d < 10) sb.append("0");
        sb.append(d); //일
        
        return sb.toString();
    }
    
    /**
     * 음력(yyyyMMdd) -> 양력(yyyyMMdd)
     * : 음력을 양력으로 변환 처리한다.
     */

    public synchronized String fromLunar(String yyyymmdd) {
        
    if( yyyymmdd == null ) return "";
        
        String date = yyyymmdd.trim();
        if( date.length() != 8) {
            if( date.length() == 4) {
                date = date + "0101";
            }else if(date.length() == 6){
                date = date + "01";                
            }else if(date.length() > 8) {
                date = date.substring(0,8);
            }else {
                return "";
            }
        }
        
        //ChinessCalendar.EXTENDED_YEAR 는 Calendar.YEAR 값과 2637 만큼의 차이를 가짐.        
        cc.set( ChineseCalendar.EXTENDED_YEAR,  Integer.parseInt(date.substring(0,4))+2637);
        cc.set( ChineseCalendar.MONTH,  Integer.parseInt(date.substring(4,6))-1);
        cc.set( ChineseCalendar.DAY_OF_MONTH,  Integer.parseInt(date.substring(6)));
        
        cal.setTimeInMillis(cc.getTimeInMillis());        
                
        int y = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH)+1;
        int d = cal.get(Calendar.DAY_OF_MONTH);
        
        StringBuffer sb = new StringBuffer();
        if( y < 1000 ) sb.append("0");
        else if(y<100) sb.append("00");
        else if(y<10) sb.append("000");
        sb.append(y);
        
        if(m < 10 ) sb.append("0");
        sb.append(m);
        
        if(d < 10) sb.append("0");
        sb.append(d);
        
        return sb.toString();    
    }
}

          http://astro.kasi.re.kr/Life/ConvertSolarLunarForm.aspx?MenuID=115