Import Java.util.TimerTask;
/**
* Executive Content
* @author ADMIN_HZW
*
*/
public class Task extends TimerTask {
public void Run () {
System.out.println ("I have a little donkey!");
}
}
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.Timer;
/**
* Task Management
* @author ADMIN_HZW
*
*/
public class Timermanager {
/**
* @param args
*/
public static void Main (string[] args) {
New Timermanager ();
}
Time interval (one day)
Private static final Long Period_day = 24 * 60 * 60 * 1000;
Public Timermanager () {
Calendar calendar = Calendar.getinstance ();
Calendar.set (Calendar.hour_of_day, 1); 1 o'clock in the morning
Calendar.set (calendar.minute, 0);
Calendar.set (Calendar.second, 0);
Date Date=calendar.gettime (); Time of the first execution of a scheduled task
If the time to perform the first scheduled task is less than the current time
At this point, the time for the first scheduled task is added to the day so that the task executes at the next point in time. If one day is not added, the task executes immediately.
if (Date.before (new Date ())) {
Date = This.addday (date, 1);
}
Timer timer = new timer ();
Task task = new Task ();
Schedules the specified task to begin repeating fixed deferred execution at a specified time.
Timer.schedule (Task,date,period_day);
}
Increase or decrease the number of days
Public date Addday (date date, int num) {
Calendar Startdt = Calendar.getinstance ();
Startdt.settime (date);
Startdt.add (Calendar.day_of_month, num);
return Startdt.gettime ();
}
}
Java timer timed daily 1 o'clock in the morning task