java--Timer

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.