How to know when will my Spring Boot scheduled job will trigger next time

Vipul Kumar
2 min readMay 21, 2020
Source: unsplash

Creating the CRON Expression

Let’s take a simple CRON Expression (“0 0 0 * * *”) which will get triggered every day at midnight.

final String CRON_EXPRESSION = "0 0 0 * * *";

Creating Sequence Generator

Now, we can use CronSequenceGenerator class present in the org.springframework.scheduling.support package to create a sequence generator instance. Let’s create an instance using its one of the constructor by providing the CRON expression we created in the last step.

CronSequenceGenerator sequenceGenerator = new CronSequenceGenerator(CRON_EXPRESSION);

Getting the next trigger point

Next, and the last step is to call next() by providing today’s date. This method will return a date object at which the CRON expression will get triggered.

Date next = sequenceGenerator.next(new Date());

Putting all in one plate

Let’s put everything togather into one code as below-

CronSequenceGenerator sequenceGenerator = new CronSequenceGenerator(CRON_EXPRESSION);

Date next = sequenceGenerator.next(new Date());

System.out.println("Next Execution Time: " + next);

One Final Call

Sometime, getting only the next date will not be helpful and we want to generate next sequence of dates when your CRON expression will get triggered. We can tweak the above code as below to get as many sequences as we want.

CronSequenceGenerator sequenceGenerator = new CronSequenceGenerator(CRON_EXPRESSION);Date next = new Date();for (int i = 0; i < 100; i++) {
next = sequenceGenerator.next(next);
System.out.println("Next Time" + next);
}

Wolla, Now we gotta a way to get the next date and time when my CRON expression will be triggered.

--

--

Vipul Kumar

A passionate software developer working on java, spring-boot and related technologies for more than 4 years.