TimerTask Scheduling upto the Minute
Hi All
Thanks for your replies to my ealier posts.
Now I have the code which can block out execution of tasks during particular hours of the day.
Is there a way I can specify upto the minute (when to block the task from running)
Like dont run the task 4:30AM - 6.15AM & 12:00 - 13:15 PM
Here is the code which can block based on hours:
import java.util.Timer;
import java.util.TimerTask;
publicclass Scheduler{
publicstaticvoid main(String[] args){
Timer timer =new Timer();
TimerTask task1 =new MyTask();
timer.schedule(task1, 1000);
Timer timer2 =new Timer();
TimerTask task2 =new MyTask2();
timer2.schedule(task2, 500);
}
}
//- MyTaskJava Code
import java.util.GregorianCalendar;
import java.util.TimerTask;
publicclass MyTaskextends TimerTask{
publicvoid run(){
System.err.println("Running My Task...");
int currentHour;
int currentMinute;
try
{
// would be a good idea to implement a general process flag
// to stop the process on any convenient moment
while(true)
{
// track the current time and evaluate if its not on the excluded interval
currentHour =new GregorianCalendar().get(GregorianCalendar.HOUR_OF_DAY);
currentMinute =new GregorianCalendar().get(GregorianCalendar.MINUTE);
System.out.println(currentHour);
if((currentHour >= 0 && currentHour < 11) || (currentHour > 14 && currentHour < 23))
{
// to implement process call
System.out.println("Its executed!");
}
// stops for 15 mins until next round
Thread.sleep(15000);
}
}
catch(Exception e)
{
// to implement exception logging
}
}
}
Please let me know.
Thanks
bib

