Hello!
I'm new in java. I need some help, guys.
How can I determine in Java next run time of cron-like job?

For example, if I have crontab entry without command, like
"*/15 * * * *", every 15 minutes
"5 */2 * * 1", every Monday at 00:05, 02:05, 04:05...

I need to determine in java, when is next run.

I've already tried jcrontab's CrontabParser class, but not found a way to determine time.

Any help appreciate.
Thanks in advance!

I've found how to solve this task!
Here is a code:

/**
* Determine next fire time of job from cron entry!
* Quartz package home page: http://quartz-scheduler.org
* Cron entry format: Seconds | Minutes | Hours | Day of month | Month Day of week | Year
* Note: Year is optional!
*/
import java.util.Calendar;
import java.util.Date;
import org.quartz.CronTrigger;

class test{
  public static void main(String[] args) throws Exception{
	Calendar cal = Calendar.getInstance();
	Date currTime = cal.getTime();
	
	CronTrigger tr = new CronTrigger();
	tr.setCronExpression("0 5 * ? * *");
	Date nextFireAt = tr.getFireTimeAfter(currTime);

	System.out.println("Current date&time: "+currTime);
	System.out.println("Next fire at: "+nextFireAt);
  }
}

Output:
Current date&time: Tue Dec 07 12:30:54 UZT 2010
Next fire at: Tue Dec 07 13:05:00 UZT 2010

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.