develog

[Spring] spring cron expression test 본문

Dev/Spring

[Spring] spring cron expression test

냐옴 2019. 6. 21. 16:46

spring cron expression test

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

import org.springframework.scheduling.support.CronSequenceGenerator;

public class TestCronExpression {

    public static void main(String[] args) {
        Calendar today = Calendar.getInstance();
        today.set(2019, 2-1, 21);
        
        testCronExpression(today, "0 30 12 27-31,1-2 * ?");
    }

    public static void testCronExpression(Calendar today, String expression) {
        System.out.println(">> testCronExpression() : " + expression);
        try {
            CronSequenceGenerator cronTrigger = new CronSequenceGenerator(expression);
            for (int i = 0; i < 62; i++) {
                Date nextExcecutionDate = cronTrigger.next(today.getTime());
                System.out.println(formatDate(today.getTime()) + " => " + formatDate(nextExcecutionDate));
                today.set(Calendar.DATE, today.get(Calendar.DATE) + 1);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public static String formatDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

}

'Dev > Spring' 카테고리의 다른 글

[Spring] beans.xml 설정3, component-scan  (0) 2015.08.28
[Spring] beans.xml 설정2, annotation-config  (0) 2015.08.28
[Spring] beans.xml 설정1, 기본  (0) 2015.08.28
[Spring] beans.xml  (0) 2015.06.04
[Spring] Spring3 AOP 설정, xml or java  (0) 2015.06.04
Comments