Test Cron Expressions

Problem Statement: How to test Cron Expressions?

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.springframework.scheduling.support.CronSequenceGenerator;

public class CronExpressionTester {

	final static TimeZone PST = TimeZone.getTimeZone("PST");

	public static void main(String[] args) {
		printNextFireTime("0 0 3 * * *");
		printNextFireTime("0 0/5 * * * *");
		printNextFireTime("0 40 15 * * *");
		
		printNextFireTime("0 5 2 * * THU,FRI");
		printNextFireTime("0 15 8,12,18 * * *");
	}

	private static void printNextFireTime(String cronExp) {
		CronSequenceGenerator csg = new CronSequenceGenerator(cronExp, PST);
		Date nextDate = csg.next(Calendar.getInstance().getTime());

		System.out.println("Next Fire Time==>" + cronExp + " ==>"
				+ DateFormat.getDateTimeInstance().format(nextDate));

	}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s