Go to >> Java Code Samples
1. How to run a Schedule Task or TimerTask
2. How to run a Schedule Task or TimerTask every day morning or every 24 hrs
1. How to run a Schedule Task or TimerTask
package basics;
import java.util.Timer;
import java.util.TimerTask;
public class TimerExamples {
public static void main(String[] args) {
Timer timer = new Timer();
//Delay for 0 mills seconds
//Repeat every 5000 mills second
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("Running....");
}
}, 0, 5000);
}
}
2. How to run a Schedule Task or TimerTask every day morning or every 24 hrs
package basics;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
public class TimerExamples {
public static void main(String[] args) {
Timer timer = new Timer();
Calendar startingTime = Calendar.getInstance(TimeZone.getDefault());
// Starting at 8:00 AM in every day the Morning
startingTime.set(Calendar.HOUR_OF_DAY, 8);
startingTime.set(Calendar.MINUTE, 00);
startingTime.set(Calendar.SECOND, 00);
timer.schedule(new TimerTask() {
public void run() {
System.out.println("Runs everday morning 8.00 AM");
}
// period 24 hrs (1000 *60 * 60 * 24 mills seconds)
}, startingTime.getTime(), 1000 * 60 * 60 * 24);
}
}
No comments:
Post a Comment