T
Tomer
If the current time is 18:50 and i schedule Repeating daily TimerTask
to 18:52 then everything is file its being run at 18:52 daily however
if i schedule it to 18:48 then its being run immediately (and i dont
want that) as i run the application
However i wanted it to be run every day at 18:48! how can i achieve
that?
following is my test code:
public class TestTimer {
public static class TestTimerTask extends TimerTask {
public void run() {
System.out.println(new Date() + " timer run...");
}
}
public static void main(String[] args) throws Exception {
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
// Timer starts yesterday (so that surely will run today when time
comes.
// date.setTimeInMillis(new Date().getTime() - 1000 * 60 * 60 * 24);
DateFormat sdf = new SimpleDateFormat("HH:mm");
Date dateToBackup = null;
try {
dateToBackup = sdf.parse("18:32");
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateToBackup);
date.set(Calendar.HOUR, calendar.get(Calendar.HOUR));
date.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
date.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
date.set(Calendar.MILLISECOND, 0);
// Schedule to run every day.
timer.schedule(
new TestTimerTask(),
date.getTime(),
1000 * 60 * 60 * 24
);
Thread.sleep(120000);
}
}
Thanks
Tomer
to 18:52 then everything is file its being run at 18:52 daily however
if i schedule it to 18:48 then its being run immediately (and i dont
want that) as i run the application
However i wanted it to be run every day at 18:48! how can i achieve
that?
following is my test code:
public class TestTimer {
public static class TestTimerTask extends TimerTask {
public void run() {
System.out.println(new Date() + " timer run...");
}
}
public static void main(String[] args) throws Exception {
Timer timer = new Timer();
Calendar date = Calendar.getInstance();
// Timer starts yesterday (so that surely will run today when time
comes.
// date.setTimeInMillis(new Date().getTime() - 1000 * 60 * 60 * 24);
DateFormat sdf = new SimpleDateFormat("HH:mm");
Date dateToBackup = null;
try {
dateToBackup = sdf.parse("18:32");
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateToBackup);
date.set(Calendar.HOUR, calendar.get(Calendar.HOUR));
date.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
date.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
date.set(Calendar.MILLISECOND, 0);
// Schedule to run every day.
timer.schedule(
new TestTimerTask(),
date.getTime(),
1000 * 60 * 60 * 24
);
Thread.sleep(120000);
}
}
Thanks
Tomer