How to Schedule a task in java?

Well,scheduling a task was made easy in java by the Timer and TimerTask classes in java.util.

Here is simple example of how to use it.

import java.util.Timer;
import java.util.TimerTask;

/**
* Schedule a task that executes every 2 seconds starting immediately.
*/
public class Scheduler1 extends TimerTask
{
// the number of times to run this task
int num = 5;

public void run()
{
if (num > 0)
{
// this is the task we are doing
System.out.println("Hello!");
num--;
}
else
{
// to terminate the currently running Java Virtual Machine
System.exit(0);
}
}

public static void main(String args[])
{
Timer timer = new Timer();

// TimerTask is a task that can be scheduled for one-time or repeated
// execution by a Timer.
TimerTask task = new Scheduler1();

// delay in milliseconds before task is to be executed.
long delay = 0;

// time in milliseconds between successive task executions,
// 2 seconds for this case
long period = 2 * 1000;

// to schedule the specified task for repeated fixed-rate execution,
// beginning after the specified delay. Subsequent executions take
// place at approximately regular intervals, separated by the
// specified period.
timer.schedule(task, delay, period);
}
}

Here is another example of how to schedule EOD jobs
http://www.javaeecoding.com/JavaUtil/Timer/Scheduler2.html