Published 2016. 1. 29. 17:40
라이브러리: icu4j 다운로드 : http://site.icu-project.org/download/48#ICU4J-Download
icu4j-4_8_1_1.jar
 
package com.tistory.yysvip.calendar;
 import java.util.Calendar;
 import org.apache.log4j.Logger;
 import com.ibm.icu.util.ChineseCalendar;
 
/**
 * 
 * com.tistory.petulantman.calendar
 *    |_ LunarCalendar.java
 *
 * 
 * @date : 2012. 4. 25. 오전 9:52:35  * @version :  * @author : 까칠한쑤  */ public class LunarCalendar {           private Logger log = Logger.getLogger(this.getClass());           private Calendar cal ;         private ChineseCalendar cc ;             public LunarCalendar() {                 // default TimeZone, Locale 을 사용..                 cal = Calendar.getInstance() ;                 cc = new ChineseCalendar();         }           /**          *
         
         * 1. 개요 : 양력(yyyyMMdd) -> 음력(yyyyMMdd)
         * 2. 처리내용 : 양력을 음력으로 변환처리한다.
         * 
         * @Method Name : toLunar          * @date : 2012. 4. 25.          * @author : 까칠한쑤          * @history :          *      -----------------------------------------------------------------------          *      변경일                          작성자                                          변경내용          *      ----------- ------------------- ---------------------------------------          *      2012. 4. 25.            까칠한쑤                                최초 작성          *      -----------------------------------------------------------------------          *          * @param yyyymmdd          * @return          */         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.YEAR 는 1~60 까지의 값만 가지고 ,                   // 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 ret = new StringBuffer() ;                 if( y < 1000 )          ret.append( "0" ) ;                 else if( y < 100 )      ret.append( "00" ) ;                 else if( y < 10 )       ret.append( "000" ) ;                 ret.append( y ) ;                   if( m < 10 ) ret.append( "0" ) ;                 ret.append( m ) ;                   if( d < 10 ) ret.append( "0" ) ;                 ret.append( d ) ;                   return ret.toString() ;         }           /**          *
         
         * 1. 개요 : 음력(yyyyMMdd) -> 양력(yyyyMMdd)
         * 2. 처리내용 : 음력을 양력으로 변환처리한다.
         * 
         * @Method Name : fromLunar          * @date : 2012. 4. 25.          * @author : 까칠한쑤          * @history :          *      -----------------------------------------------------------------------          *      변경일                          작성자                                          변경내용          *      ----------- ------------------- ---------------------------------------          *      2012. 4. 25.            까칠한쑤                                최초 작성          *      -----------------------------------------------------------------------          *          * @param yyyymmdd          * @return          */         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 "" ;                 }                   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 ret = new StringBuffer() ;                 if( y < 1000 )          ret.append( "0" ) ;                 else if( y < 100 )      ret.append( "00" ) ;                 else if( y < 10 )       ret.append( "000" ) ;                 ret.append( y ) ;                   if( m < 10 ) ret.append( "0" );                 ret.append( m ) ;                   if( d < 10 ) ret.append( "0" );                 ret.append( d ) ;                   return ret.toString() ;         }           public static void main(String args[])         {                 LunarCalendar lc = new LunarCalendar() ;                   System.out.println("2012년 4월 25일에 대한 음력날짜는 아래와 같습니다.");                 System.out.println( lc.toLunar("20120425") ) ; // 양력을 음력으로 바꾸기                   System.out.println("2012년 4월 05일에 대한 양력날짜는 아래와 같습니다.");                 System.out.println( lc.fromLunar("20120405") ) ; // 2012년 4월 25일에 대한 음력날짜를 집어넣는다.         } // end class }
출처 : http://yysvip.tistory.com/159
복사했습니다!