How i can run my TimerTask everyday 2 PM

后端 未结 9 1652
说谎
说谎 2020-12-01 02:48

I want to execute a job everyday 2PM . Which method of java.util.Timer i can use to schedule my job?

After 2Hrs Run it will stop the job and reschedule

相关标签:
9条回答
  • 2020-12-01 03:29

    You should try using scheduleAtFixedRate (this will repeat your task). You will need to create an TimerTask object which will specify what to run (in run()) and when to run (scheduledExecutionTime). scheduleAtFixedRate also allows you to specify the first date of execution.

    0 讨论(0)
  • 2020-12-01 03:30

    You could use Timer.schedule(TimerTask task, Date firstTime, long period) method, setting firstTime to 2PM today and the setting the period to 24-hours:

    Schedules the specified task for repeated fixed-delay execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

    0 讨论(0)
  • 2020-12-01 03:32

    use public void schedule(TimerTask task,Date firstTime,long period)

    to make the task repeats again the next day, just set period to 86400000 milliseconds ( which means 1 day )

    Date date2pm = new java.util.Date();
    date2pm.setHour(14);
    date2pm.setMinutes(0);
    
    Timer timer = new Timer();
    
    timer.sc(myOwnTimerTask,date2pm, 86400000);
    
    0 讨论(0)
提交回复
热议问题