Java/Language
자바 음력날짜 얻기
허니몬
2011. 10. 11. 16:51
관련 사이트 :
- Joda Time : http://joda-time.sourceforge.net/quickstart.html
- ICU4j calendar : http://userguide.icu-project.org/datetime/calendar#TOC-Calendar
쪼다타임(Joda time)은 자바의 Date, Time class를 대신할 수 있는 DateTime 클래스를 제공한다. 날짜, 시간 정보를 손쉽게 접근하고 가공할 수 있는 오픈소스다. 퀵스타트만 봐도 손쉽게 사용할 수 있다. ^^;
쪼다타임이 편리하기는 한데, 음력 처리를 해주지 못하는 걸 알고는 인터넷을 뒤져보다가 IBM에서 ICU(International Componenets for Unicode)라는 이름으로 제공하는 컴포넌트다. 그 중에 달력처리 부분에서 ChineseCalendar를 이용해서 음력처리가 가능한 것을 발견하고는 간단하게 테스트 코드를 작성해본다.
/**
* Create Date : 2011. 10. 11.
*/
package honeymon.study.test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import com.ibm.icu.util.ChineseCalendar;
/**
* @author 허니몬
*/
public class LunarCalendarTest {
private ChineseCalendar chineseCal;
private DateTime dateTime;
@Before
public void setUp() {
chineseCal = new ChineseCalendar();
dateTime = new DateTime();
}
/**
* 왜 중국음력에서 달에 +1을 하는 걸까?
* +1은 회합주기?
* 2011/10/11 음력은 2011/09/15일
*/
@Test
public void 음력테스트() {
chineseCal.setTimeInMillis(dateTime.getMillis());
assertThat(chineseCal.get(ChineseCalendar.MONTH) + 1, is(9));
assertThat(chineseCal.get(ChineseCalendar.DAY_OF_MONTH), is(15));
}
}