목차

크론정리II

  • description : 크론정리
  • author : 오션
  • email : shlim@repia.com
  • lastupdate : 2023-03-14 Tue


크론정리II

0 59 23 8 * ?


cRonstrue에서 상기 크론식 입력 후 결과는
⇒ “At 11:59 PM, on day 8 of the month”
⇒ “매월 8일 오후 11시 59분” (구글 번역기)

cron Method

public static boolean isMatchCronExpression(String cronString, int dayOffset) throws ParseException, Exception {
 
    boolean bRet = false;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
 
    try
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, dayOffset);
 
        CronExpression cronExp = new CronExpression(cronString);
        Date cronDate = cronExp.getNextValidTimeAfter(cal.getTime());
 
        String cronDateStr = dateFormat.format(cronDate);
 
        String compareDateStr = dateFormat.format(cal.getTime());
 
        return compareDateStr.equals(cronDateStr);
    }
    catch(ParseException ePe)
    {
        throw ePe;
    }
    catch(Exception eCommon)
    {
        log.error("cronMatch error", eCommon);
    }
 
    return bRet;
}


cron Test Method Detail

@Test
public void cronTest() {
 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
 
    String userCrontabInputText = "8 * ?";
 
    String cronStr = "0 59 23 " + userCrontabInputText;
 
    int userAlarmDt = -2;    // -2는 '2일 전 08시'를 의미함
 
    try
    {
        Calendar cal = Calendar.getInstance();    //캘린더 객체 생성
        log.debug("1. cal = [{}]", cal);
        //1. cal = [java.util.GregorianCalendar[time=1678751883285,areFieldsSet=true,areAllFieldsSet=true,lenient=true,
	//zone=sun.util.calendar.ZoneInfo[id="Asia/Seoul",offset=32400000,dstSavings=0,useDaylight=false,transitions=30,
	//lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2023,MONTH=2,WEEK_OF_YEAR=11,WEEK_OF_MONTH=3,
	//DAY_OF_MONTH=14,DAY_OF_YEAR=73,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=58,SECOND=3,
	//MILLISECOND=285,ZONE_OFFSET=32400000,DST_OFFSET=0]]
 
        cal.setTime(ndw Date());                                //현재 날짜시간으로 캘린터 객체 cal을 설정
        log.debug("2. cal.getTime() = [{}]", cal.getTime());    //2. cal = [2023-03-14T08:58:03.295+0900]
 
        cal.add(Calendar.DATE, userAlarmDt);                    //
 
 
 
 
 
    }
 
 
 
 
 
}

cRonstrue-translate cron expression to human readable descriptions