Base code:
Calendar currenttime = Calendar.getinstance ();
Currenttime.settime (New Date ());
int currenthour = Currenttime.get (Calendar.second);
Currenttime.set (Calendar.second, Currenthour + 20);//First (greater than 20 seconds): Currenthour + 20; second (less than 20 seconds): Currenthour- 20; third (equals current): Currenthour
Currenttime.set (Calendar.millisecond, 0);
Date nexthour = Currenttime.gettime ();
Explain:
Nexthour
1*60*1000: one-minute cycle
--------------------------------------------------------------------------------------------------------------- --------
1. Experiments with Timer.scheduleatfixedrate (task, firsttime, period)
Timer.scheduleatfixedrate (New Myhouronlinetask (Event.getservletcontext ()), nexthour,1*60*1000);
Experimental results:
Hypothetical condition: The time after project start OK is 2015-01-08 10:00:00!
First: 2015-01-08 10:00:20 when the scheduled task executes once, and then starts from 2015-01-08 10:00:20, every 1 minutes.
Second: 2015-01-08 10:00:00 the scheduled task executes once, and then starts from 2015-01-08 10:00:40, and executes every 1 minutes.
Third: 2015-01-08 10:00:00 when the scheduled task executes once, every 1 minutes.
Remark Timer.scheduleatfixedrate (New Myhouronlinetask (Event.getservletcontext ()), 0,1*60*1000); 2015-01-08 10:00:00 timed tasks are executed once, every 1 minutes.
2. Experiments with Timer.schedule (task, firsttime, period)
Experimental results:
Hypothetical condition: The time after project start OK is 2015-01-08 10:00:00!
First: 2015-01-08 10:00:20 when the scheduled task executes once, and then starts from 2015-01-08 10:00:20, every 1 minutes.
Second: 2015-01-08 10:00:00 the scheduled task executes once, every 1 minutes.
Third: 2015-01-08 10:00:00 when the scheduled task executes once, every 1 minutes.
Note Timer.schedule (New Myhouronlinetask (Event.getservletcontext ()), 0,1*60*1000); when, 2015-01-08 10:00:00 the scheduled task executes once, Executes every 1 minutes.
Experiments of Timer.scheduleatfixedrate and Timer.schedule