Schedule Java task for a specified time

Ray picture Ray · Nov 9, 2011 · Viewed 12.2k times · Source

I would like to be able to schedule a task at a specific time in Java. I understand that the ExecutorService has the ability to schedule at periodic intervals, and after a specified delay, but I am looking more for a time of day as opposed to after a duration.

Is there a way to have, say, a Runnable execute at 2:00, or do I need to calculate the time between now and 2:00, and then schedule the runnable to execute after that delay?

Answer

Peter Szanto picture Peter Szanto · Nov 9, 2011

you can use spring annotations too

@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// something that should execute on weekdays only
}

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html