Friday, September 9, 2011
Create Scheduler in Java
Some applications need to run on a scheduled time. This java application runs on every Sunday midnight.
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class ScheduleTest extends TimerTask {
public void run() {
System.out.println("Schedule working ");
//TODO
}
}
class Application {
public static void main(String[] args) {
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
date.set(
Calendar.DAY_OF_WEEK,
Calendar.SUNDAY
);
date.set(Calendar.HOUR, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// Schedule to run every Sunday in midnight
timer.schedule(
new ScheduleTest (),
date.getTime(),
1000 * 60 * 60 * 24 * 7
);
}
}