package jp.agentec.sinaburocast.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.seasar.framework.util.StringUtil;

public class BirthdayUtil {
	
	/**
	 * 年齢を返す
	 * @param birth_day yyyyMMdd 形式の誕生日
	 * @return 年齢
	 * @throws ParseException
	 */
	public static int getOld(String birth_day) throws ParseException {

		if(StringUtil.isBlank(birth_day)) return 0;

	    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

	    Calendar cal = Calendar.getInstance();
	    cal.setTime(df.parse(birth_day));

	    Date now = new Date();
	    if (cal.getTime().after(now)) {
	        return 0; // マイナスは0
	    }
	    Calendar nowCal = Calendar.getInstance();
	    nowCal.setTime(now);

	    String nowYmd = df.format(now);
	    int old = (Integer.parseInt(nowYmd) - Integer.parseInt(birth_day)) / 10000;
	    return old;
	}
	
	
	public static boolean isLessAgeRestriction(String birth_day) throws ParseException {
		return (getOld(birth_day) < PropertyUtil.getShort("age.restriction"));
	}
}