Reprint other people's, for later study.
Import java.util.timertask;/** * Execute content * @author ADMIN_HZW * */public class Task extends TimerTask {public void run () {Syst Em.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 Timermanag ER (); }//interval (one day) private static final Long Period_day = $ * 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);D ate date=calendar.gettime (); Time of the first execution of a scheduled task//If the time of the first execution of the scheduled task is less than the current time//The time of the first execution of the 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 ();//Schedule 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 is scheduled to perform tasks 1 o'clock in the morning every day