Need help on Task Scheduling using Timer task

Hi all,

Does any body know how to schedule a task at any given time using Timertask. Say i want to execute a task at 4:00 AM. but the timer task is executing the task at 4:00 AM and as well as at 4:00 PM also. But i don't want to execute the task at 4:00 PM. So how to specify the start time exactly as 4:00 AM only.How to specify the exact start time for timer task . please refer the following code how i have implemented

Looking for any kind of help or hints or suggestions.

Thanks in advance

Date startTime = getExecutionTime(4,0);

timer=new java.util.Timer();

ImportingTask task = new ImportingTask();

timer.scheduleAtFixedRate(task,startTime,interval);

private Date getExecutionTime(int hours,int minutes ){

GregorianCalendar present_Cal = new GregorianCalendar();

GregorianCalendar result = new GregorianCalendar(

present_Cal.get(Calendar.YEAR),

present_Cal.get(Calendar.MONTH),

present_Cal.get(Calendar.DATE),

hours,

minutes );

return result ;

}

class ImportingTask extends TimerTask{

public void run(){

executeScheduledJob();

}

}

[1218 byte] By [kbmkchowdary] at [2007-9-30 13:49:33]
# 1
What did you specify in line timer.scheduleAtFixedRate(task,startTime,interval); for the interval?It should be 24(hours)*60(minutes)*60(sec)*1000(millisecs)=86400000to make the task to be scheduled ecery 24 hours.
FahleE at 2007-7-4 23:26:46 > top of Java-index,Administration Tools,Sun Connection...
# 2

Hi

I Need the task to be Started at 4.00 AM and it need to run at an interval of 1 hour , that means the task should need to be execute at 4:00 AM, 5:00 AM and soo on ...... so i gev the the interval as3600000 milli seconds.

timer.scheduleAtFixedRate(task,startTime,interval);

But my problem is the execution starts at 4.00 PM also, But i want the execution should be at 4.00 AM only

Looking for your advice Thanks alot in advance

kbmkchowdary at 2007-7-4 23:26:46 > top of Java-index,Administration Tools,Sun Connection...
# 3

Hi

I Need the task to be Started at 4.00 AM and it need to run at an interval of 1 hour , that means the task need to be execute at 4:00 AM, 5:00 AM and soo on ...... so i gave the the interval as3600000 milli seconds.

timer.scheduleAtFixedRate(task,startTime,interval);

But the problem is the execution starts at 4.00 PM also, But i want the execution should be at 4.00 AM only

Looking for your advice Thanks alot in advance

kbmkchowdary at 2007-7-4 23:26:46 > top of Java-index,Administration Tools,Sun Connection...
# 4

Sorry, I really don't understand the schedule of your timer?

You want to start it (first execution) at 4:00 AM, and then at an interval of 1 hour, is that right?

Well, then after 12 hours (its 4:00 PM by now) it will execute again!

Maybe you should post an exact timetable (24 hours) for the exceutions.

FahleE at 2007-7-4 23:26:46 > top of Java-index,Administration Tools,Sun Connection...
# 5

Modify the GregorianCalendar that you use for start time by calling .set(Calendar.HOUR_OF_DAY, 4) (this is the 24hour version, as opposed to Calendar.HOUR set in constructor, I think), then make sure your interval is for 24 hours as above (I make 86400000). Then it should only run at 4AM

masseyis2 at 2007-7-4 23:26:46 > top of Java-index,Administration Tools,Sun Connection...
# 6

I also have a similar problem.

I used timer.scheduledAtFixedRate(MyTimerTask, start date, period). The start date is today 6:00AM and the period is 24 hours. It works fine. My problem is that I would like the user to enter a specific number of minutes or hours before it should start again.

The current program starts initially at 6:00AM then it stops after 24 hours (which is 6:00AM the next day) and runs again.

What I would like is that after 24 hours, the user can input, say, 5 mins......thus, the program should run at 6:05AM. Then it should run again at 6:10AM the next day and so forth.

Can scheduledAtFixedRate handle this? Thanks in advance!

shibby1011ph at 2007-7-4 23:26:46 > top of Java-index,Administration Tools,Sun Connection...