JAVA

[JAVA] 음력 양력 변환 라이브러리

kwh_coding 2023. 9. 20. 13:25

1.  음력변환 라이브러리 추가

설, 추석과 같은 음력 공휴일을 처리하기 위해 음력변환 라이브러리를 추가해 준다.

 <!-- https://mvnrepository.com/artifact/com.ibm.icu/icu4j -->
        <dependency>
            <groupId>com.ibm.icu</groupId>
            <artifactId>icu4j</artifactId>
            <version>4.0.1</version>
        </dependency>

 

1.  음력 공휴일 체크

아래는 음력 공휴일을 체크하는 소스이다.

 

import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.ChineseCalendar;


public class HolidayCheck {
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		// 체크할 양력날짜
		String dt = "2014-09-09" ;
		
		
		try {
			
			// 결과값 출력
			System.out.println(isLunar(dt)) ;
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}
    
    private static boolean isLunar(String dt) throws Exception{
		
		boolean result = false ;
		
		
		// 음력 공휴일 목록
		String[] arrLunar = {
				
				"01-01"		// 설날 2 
				, "01-02"	// 설날 3
				, "04-08"	// 부처님 오신날
				, "08-14"	// 추석 1
				, "08-15"	// 추석 2
				, "08-16"	// 추석 3
				, "12-31"	// 설날 1
		} ;
		
		
		
		ChineseCalendar chinaCal = new ChineseCalendar();
		Calendar cal = Calendar.getInstance() ;
		
		cal.set(Calendar.YEAR, Integer.parseInt(dt.substring(0, 4)));
		cal.set(Calendar.MONTH, Integer.parseInt(dt.substring(5, 7)) - 1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dt.substring(8,10)));
		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);
		
		
		
		
		String chinaDate = "" ;		// 음력 날짜
		
        
		if(chinaDD < 10)			// 일
			chinaDate += "0" + Integer.toString(chinaDD) ;
		else
			chinaDate += Integer.toString(chinaDD) ;
		
		
		
		// 음력 공휴일 목록과 변환한 음력날짜가 일치하는지 비교
		for(int i=0; i < arrLunar.length; i++){
			String tmpLunar = arrLunar[i] ;
			
			if(tmpLunar.equals(chinaDate)){
				result = true ;
				
			}

		}
		
		return result ;
		
	}

}