Packagecom.test;ImportJava.text.Format;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classHH { Public Static voidMain (string[] args) {//timer1 ();Date Date=NewDate (); Calendar Calendar=calendar.getinstance (); Format Formatter=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Time=Formatter.format (date); System.out.println ("Start time:" +Time ); //timer1 (); //Timer2 ();Timer3 (); //timer4 (); } /*** The first method: set the specified task to execute schedule at a specified time (timertask task, Date time) * Only executes the task after the execution of the program, and executes it only once*/ Public Static voidtimer1 () {Timer timer=NewTimer (); Timer.schedule (NewTimerTask () { Public voidrun () {System.out.println ("-------set the task to be specified--------"); } }, 2000);//sets the time specified, here is 2000 milliseconds } /*** Second method: Sets the execution of fixed delay peroid for the specified task tasks after the specified delayed delay*/ //Schedule (timertask task, long delay, long period) Public Static voidTimer2 () {Timer timer=NewTimer (); Timer.schedule (NewTimerTask () { Public voidrun () {//System.out.println ("-------set to specify task--------");Date date=NewDate (); Calendar Calendar=calendar.getinstance (); Format Formatter=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Time=Formatter.format (date); System.out.println (time); } }, 1000, 2000);//Start execution after 1 seconds, then scan every two seconds (2000ms) } /*** The Third method: sets the execution of a fixed frequency peroid for the specified task tasks after the specified delayed delay. */ //scheduleatfixedrate (timertask task, long delay, long period) Public Static voidTimer3 () {Timer timer=NewTimer (); Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System.out.println ("-------set the task to be specified--------"); } }, 1000, 2000);//Start execution after 1 seconds, then scan every two seconds (2000ms) } /*** Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat. */ //timer.scheduleatfixedrate (timertask task,date firsttime,long period) Public Static voidtimer4 () {Calendar Calendar=calendar.getinstance (); Calendar.set (Calendar.hour_of_day,12);//Control TimeCalendar.set (calendar.minute, 0);//Control PointsCalendar.set (Calendar.second, 0);//Control secondsDate Time= Calendar.gettime ();//The time to execute the task, here is today's 12:00:00Timer Timer=NewTimer (); Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System.out.println ("-------set the task to be specified--------"); }}, Time,1000*60*60 * 24);//This setting will delay the execution of the daily fixed }}
Packagecom.test;ImportJava.text.Format;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classHH { Public Static voidMain (string[] args) {//timer1 ();Date Date=NewDate (); Calendar Calendar=calendar.getinstance (); Format Formatter=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Time=Formatter.format (date); System.out.println ("Start time:" +Time ); //timer1 (); //Timer2 ();Timer3 (); //timer4 (); } /*** The first method: set the specified task to execute schedule at a specified time (timertask task, Date time) * Only executes the task after the execution of the program, and executes it only once*/ Public Static voidtimer1 () {Timer timer=NewTimer (); Timer.schedule (NewTimerTask () { Public voidrun () {System.out.println ("-------set the task to be specified--------"); } }, 2000);//sets the time specified, here is 2000 milliseconds } /*** Second method: Sets the execution of fixed delay peroid for the specified task tasks after the specified delayed delay*/ //Schedule (timertask task, long delay, long period) Public Static voidTimer2 () {Timer timer=NewTimer (); Timer.schedule (NewTimerTask () { Public voidrun () {//System.out.println ("-------set to specify task--------");Date date=NewDate (); Calendar Calendar=calendar.getinstance (); Format Formatter=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Time=Formatter.format (date); System.out.println (time); } }, 1000, 2000);//Start execution after 1 seconds, then scan every two seconds (2000ms) } /*** The Third method: sets the execution of a fixed frequency peroid for the specified task tasks after the specified delayed delay. */ //scheduleatfixedrate (timertask task, long delay, long period) Public Static voidTimer3 () {Timer timer=NewTimer (); Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System.out.println ("-------set the task to be specified--------"); } }, 1000, 2000);//Start execution after 1 seconds, then scan every two seconds (2000ms) } /*** Fourth method: Schedule the specified task to execute at a fixed rate period the specified time Firsttime begins to repeat. */ //timer.scheduleatfixedrate (timertask task,date firsttime,long period) Public Static voidtimer4 () {Calendar Calendar=calendar.getinstance (); Calendar.set (Calendar.hour_of_day,12);//Control TimeCalendar.set (calendar.minute, 0);//Control PointsCalendar.set (Calendar.second, 0);//Control secondsDate Time= Calendar.gettime ();//The time to execute the task, here is today's 12:00:00Timer Timer=NewTimer (); Timer.scheduleatfixedrate (NewTimerTask () { Public voidrun () {System.out.println ("-------set the task to be specified--------"); }}, Time,1000*60*60 * 24);//This setting will delay the execution of the daily fixed }}
Original source: http://blog.csdn.net/bzuld/article/details/8598776
java--Timer