dcsimg

How do I schedule a task to run at a certain time?

John Zukowski

Use the schedule() method of the java.util.Timer class:


long now = System.currentTimeMillis();
Date whenToRun = new Date(now+millisecondsInFuture);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
  public void run() {
    // job code here
  }
};
timer.schedule(task, whenToRun);