How i can run my TimerTask everyday 2 PM

BOSS picture BOSS · Feb 21, 2012 · Viewed 90.4k times · Source

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 for next day 2PM.

Answer

Daniel Gerber picture Daniel Gerber · Aug 7, 2013
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 2);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);

// every night at 2am you run your task
Timer timer = new Timer();
timer.schedule(new YourTask(), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)); // period: 1 day