반응형


자바에서 달력을 만들어 보는 기본 코드이다.


다음은 달력을 만들기 위한 조건이다.


1) 달력 프로그램의 입력을 xxxxxxxx일로 한다.

2) 잘못된 값을 입력 시 예외처리

3) 입력날짜의 요일과 음력을 출력한다.



알고리즘은 다음과 같다.


<Calendar.java>

- 입력은 ‘xxxx년 xx월 xx일’과 같이 받아 내도록 한다. (ex : 0500년 01월 03일)

  이때 이 양식과 다르게 값을 입력받는다면 예외 처리를 출력한다.


- java에는 달력을 쉽게 이용할 수 있도록 라이브러리가 존재한다고 들었지만, 자바를 처음 접해보는 입장에 서서 따로 라이브러리를 이용하지 않고 날짜 구하는 식을 이용하여 제작한다.


- 입력 받는 값은 String으로 받아내고, 년, 월, 일에 맞도록 잘라내어 배열에 넣은 후, 다시 String에 넣어 Integer.parseInt를 이용하여 마지막으로 숫자로 받아낸다. 

  예를 들어 2015년이면 2015년의 2015만 배열에 넣어두고, 배열에 있는 2015를 String으로 바꾸어내서, Integer.parseInt를 이용하여 year int형 변수에 넣는다.


- 윤년 구분 코드는 year % 4 == 0 && year % 100 != 0 || year % 400 == 0를 이용한다. 

  그리고 01년 01월 01일부터일수를 계산하는 코드는 day += (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100  + (year - 1) / 400;를 이용한다.


<lunarCalendar.java>

- 음력을 구하는 공식은 존재하지 않고, 데이터 베이스에 직접 기입하는 방식이라고 검색 결과 판단하였다. 

  따라서 ICU4J - International Components for Unicode for Java를 이용하여 Chinese Calendar(음력 달력)을 받아내었고, 코드를 이용, 응용하였다.


  참고자료 : http://fruitdev.tistory.com/73



코드


<Calendar.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

import java.util.Scanner;
 
public class Calendar {
 
    static int year;
    static int month;
    static int day;
    static int ansYoil = 0;
    static Scanner scan = new Scanner(System.in);
    static int monthArr[] = {31,28,31,30,31,30,31,31,30,31,30,31};        
    static String tmp;
    static char arr[];
    
 
    public static void main(String[] args) {
        
 
        lunarCalendar LC = new lunarCalendar();
 
        System.out.println("xxxx년 xx월 xx일 양식으로 입력해주세요.");
        System.out.println("ex :: 2016년 02월 01일");
        getValue(); // 년 월 일 받아온다.
        
        // 윤년 검증 
        if (year % == && year % 100 != || year % 400 == 0)
            monthArr[1= 29//윤년이 아닐 때
         
        else
            monthArr[1= 28//윤년
            
        
        // 01년 01월 01일부터 일수 계산
        day += (year - 1* 365 + (year - 1/ - (year - 1/ 100  + (year - 1/ 400;
         
        for (int i = 0; i < month - 1; i++)
            day += monthArr[i];
            
       ansYoil = day % 7;
       
       switch(ansYoil)
       {
          case 0:
               {System.out.print("일요일, "); break;}
 
           case 1:
               {System.out.print("월요일, "); break;}
 
           case 2:
               {System.out.print("화요일, "); break;}
 
           case 3:
               {System.out.print("수요일, "); break;}
 
           case 4:
               {System.out.print("목요일, "); break;}
 
           case 5:
               {System.out.print("금요일, "); break;}
           
           case 6:
               {System.out.print("토요일, "); break;}
       }
 
        ((lunarCalendar) LC).getLunar(tmp);
    }
 
    
    private static void getValue() 
    {
 
        char tmpYear[] = new char[4];
        char tmpMonth[] = new char[2];
        char tmpDay[] = new char[2];
        int k;
        
        String sTmp;
        
        // 값을 입력 받는다.
        tmp = scan.nextLine();        
       
        // 값을 배열로 바꾼다.
        arr = tmp.toCharArray();
        
        // 년도를 문자에서 숫자로 변환
        for(k = 0; k < 4; k++)
        {
            if'0' <= arr[k] && arr[k] <= '9')
            tmpYear[k] = arr[k];
            
            // 년도를 잘못 기입할 때 예외처리
            else
            {
                System.out.println("년도에 숫자가 아닌 다른 문자가 있습니다. 다시 입력하세요.");
                getValue();
                return;
            }
        }
        
        // 한글 '년'을 잘못 입력할 때 예외처리
        if(!tmp.substring(45).equals("년"))
        {
            System.out.println("'년' 입력이 잘못되었습니다. 다시 입력하세요.");
            getValue();
            return;
        }
        
        sTmp = new String(tmpYear);        
        year = Integer.parseInt(sTmp);
        
        // 월을 문자에서 숫자로 변환
        for(k = 6; k < 8; k++)
        {
            if('0' <= arr[k] && arr[k] <= '9')                
                tmpMonth[k-6= arr[k];
            
            // 월을 잘못 기입할 때 예외처리
            else
            {
                System.out.println("월에 숫자가 아닌 다른 문자가 있습니다. 다시 입력하세요.");
                getValue();
                return;
            }
        }
        
        // 한글 '월'을 잘못 입력할 때 예외처리
        if(!tmp.substring(89).equals("월"))
        {
            System.out.println("'월' 입력이 잘못되었습니다. 다시 입력하세요.");
            getValue();
            return;
        }
        
        sTmp = new String(tmpMonth);
        month = Integer.parseInt(sTmp);
        
 
        // 일을 문자에서 숫자로 변환
        for(k = 10; k < 12 ; k++)
        {
            if('0' <= arr[k] && arr[k] <= '9')
                tmpDay[k-10= arr[k];
        
            // 일을 잘못 기입할 때 예외처리
            else
            {
                System.out.println("일에 숫자가 아닌 다른 문자가 있습니다. 다시 입력하세요.");
                getValue();
                return;
            }            
        }
        
        // 한글 '일'을 잘못 입력할 때 예외처리
        if(!tmp.substring(1213).equals("일"))
        {
            System.out.println("'일' 입력이 잘못되었습니다. 다시 입력하세요.");
            getValue();
            return;
        }
        
        sTmp = new String(tmpDay);
        day = Integer.parseInt(sTmp);
        
        
        if (year < || (month < || month > 12|| (day < || day > 31)) 
        {
            System.out.println("년, 월, 일중 맞지 않은 값이 있습니다. 다시 입력하세요.");
            getValue();
            return;
        }        
 
    }
 
 
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus




<lunarCalendar.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
package JavaReport_P;
 
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ChineseCalendar;
 
 
public class lunarCalendar {
 
    public void getLunar(String today) {
         
        ChineseCalendar chinaCal = new ChineseCalendar();
        Calendar cal = Calendar.getInstance() ;
         
        cal.set(Calendar.YEAR, Integer.parseInt(today.substring(04)));
        cal.set(Calendar.MONTH, Integer.parseInt(today.substring(68)) - 1);
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(today.substring(10,12)));
        chinaCal.setTimeInMillis(cal.getTimeInMillis());
         
 
        int chinaYY = chinaCal.get(ChineseCalendar.EXTENDED_YEAR) - 2637 ;
        int chinaMM = chinaCal.get(ChineseCalendar.MONTH) + 1;
        int chinaDD = chinaCal.get(ChineseCalendar.DAY_OF_MONTH);
         
        System.out.print("음력 "); 
        String chinaDate = "" ;     // 음력 날짜
         
        chinaDate += chinaYY ;      // 년
        chinaDate += "년 " ;          // 연도 구분자         
         
        if(chinaMM < 10)         // 월
            chinaDate += "0" + Integer.toString(chinaMM) ;
        else
            chinaDate += Integer.toString(chinaMM) ;
         
         
        chinaDate += "월 " ;          // 날짜 구분자
         
         
        if(chinaDD < 10)         // 일
            chinaDate += "0" + Integer.toString(chinaDD) ;
        else
            chinaDate += Integer.toString(chinaDD) ;
         
         
         
        System.out.println(chinaDate + "일 ") ;
         
 
    }
 
 
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus




예외 처리를 표현하고, 가장 아래에 결과값이 나타난다.



네이버의 음력 계산기와 동일함을 알 수 있다.



반응형

'Basic > Java' 카테고리의 다른 글

Java 출력 기초 ( System.out.print, println, printf)  (0) 2016.10.12
자바 String 기본 함수  (0) 2016.10.07
Eclipse jar 파일 import 하기  (0) 2016.09.22
배열을 이용할 때 주의할 점  (0) 2016.09.21
Eclipse 몇 가지 유용한 팁  (0) 2016.06.20