package common.util; import java.sql.Date; import org.joda.time.DateTime; import org.joda.time.Years; /** *
 * 시스템명 : 차세대정보시스템(S/W)
 * 업무구분 : 공통 (cn)
 * 업 무 명 : 공통 API
 * 파 일 명 : AgeUtils.java
 * 작 성 자 : Application Architect
 * 작 성 일 : 2014.05.07
 * 설    명 : 현재 혹은 기준 일자를 기준으로 한국 나이, 만 나이를 계산하는 유틸리티 메소드들을 제공한다.
 * 
*/ public final class AgeUtils { private AgeUtils() { // This class does not provider public constructor } /** * 현재 날짜를 기준으로 생년월일에서 한국식 나이를 구한다. * *
	 * examples
	 * AgeUtils.getAge("19731201"); = 41 (현재 년도가 2013년인 경우)
	 * 
* * @param birthDate 생년월일 (yyyyMMdd 형식) * @return 현재 일자를 기준으로 계산된 한국식 나이 * @throws IllegalArgumentException null 혹은 잘못된 형식의 날짜를 입력한 경우 예외 발생 */ public static int getAge(String birthDate) { return getAge(birthDate, DateUtils.getTodayString()); } /** * 기준일자를 기준으로 생년월일에서 한국식 나이를 구한다. * *
	 * examples
	 * AgeUtils.getAge("19731201", "20121225"); = 40
	 * 
* * @param birthDate 생년월일 (yyyyMMdd 형식 혹은 yyyy 형식) * @param refDate 기준일자 (yyyyMMdd 형식 혹은 yyyy 형식) * @return 한국식 나이 * @throws IllegalArgumentException null 혹은 잘못된 형식의 날짜를 입력한 경우 예외 발생 */ public static int getAge(String birthDate, String refDate) { if (StringUtils.isEmpty(birthDate) || StringUtils.isEmpty(refDate)) { throw new IllegalArgumentException("birthDate or refDate parameter is empty."); } else if (birthDate.length() < 4 || refDate.length() < 4) { throw new IllegalArgumentException("birthDate or refDate parameter length is too short (must longer than 3)."); } else if (!StringUtils.isNumeric(birthDate) || !StringUtils.isNumeric(refDate)) { throw new IllegalArgumentException("birthDate or refDate parameter is not numeric data."); } if (birthDate.length() == 4) { return Integer.parseInt(refDate) - Integer.parseInt(birthDate) + 1; } else { return getAge(DateUtils.toDate(birthDate), DateUtils.toDate(refDate)); } } /** * 생년월일과 기준 일자를 비교하여 한국식 나이를 반환한다. * *
	 * examples
	 * Date birthDate = DateUtils.toDate("20120101");
	 * AgeUtils.getAge(birthDate, DateUtils.toDate("20120201")); = 1
	 * AgeUtils.getAge(birthDate, DateUtils.toDate("20130101")); = 2
	 * 
* * @param birthDate 생년월일 (yyyyMMdd 형식) * @param refDate 기준일자 (yyyyMMdd 형식) * @return 한국식 나이 * @throws IllegalArgumentException null 혹은 잘못된 형식의 날짜를 입력한 경우 예외 발생 */ public static int getAge(Date birthDate, Date refDate) { if (birthDate == null || refDate == null) { throw new IllegalArgumentException("Invalid argument value. birthDate = '" + birthDate + "', refDate = '" + refDate + "'"); } else { return new DateTime(refDate).getYear() - new DateTime(birthDate).getYear() + 1; } } /** * 현재 일자를 기준으로 생년월일에서 만 나이를 구한다. * *
	 * examples
	 * AgeUtils.getRealAge("19731201"); = 39 (현재 년도가 2013년인 경우)
	 * 
* * @param birthDate 생년월일 (yyyyMMdd 형식) * @return 만 나이 * @throws IllegalArgumentException null 혹은 잘못된 형식의 날짜를 입력한 경우 예외 발생 */ public static int getRealAge(String birthDate) { return getRealAge(birthDate, DateUtils.getTodayString()); } /** * 생년월일과 기준 일자를 비교하여 만 나이를 계산한다. * *
	 * examples
	 * AgeUtils.getRealAge("19731201", "20121125"); = 38
	 * AgeUtils.getRealAge("19731201", "20121225"); = 39
	 * 
* * @param birthDate 생년월일 (yyyyMMdd 형식) * @param refDate 기준일자 (yyyMMdd 형식) * @return 만 나이 * @throws IllegalArgumentException null 혹은 잘못된 형식의 날짜를 입력한 경우 예외 발생 */ public static int getRealAge(String birthDate, String refDate) { return getRealAge(DateUtils.toDate(birthDate), DateUtils.toDate(refDate)); } /** * 생년월일과 기준 일자를 비교하여 만 나이를 계산한다. * *
	 * examples
	 * Date birthDate = DateUtils.toDate("20120101");
	 * AgeUtils.getRealAge(birthDate, DateUtils.toDate("20120201")); = 0
	 * AgeUtils.getRealAge(birthDate, DateUtils.toDate("20130101")); = 1
	 * 
* * @param birthDate 생년월일 (Date 타입) * @param refDate 기준일자 (Date 타입) * @return 만 나이 * @throws IllegalArgumentException null 혹은 잘못된 형식의 날짜를 입력한 경우 예외 발생 */ public static int getRealAge(Date birthDate, Date refDate) { if (birthDate == null || refDate == null) { throw new IllegalArgumentException("Invalid argument value. birthDate = '" + birthDate + "', refDate = '" + refDate + "'"); } else { return Years.yearsBetween(new DateTime(birthDate), new DateTime(refDate)).getYears(); } } }