Java timer timed daily 0 o'clock in the morning task

Source: Internet
Author: User

import java.util.TimerTask;   /* *  * * * * * *  @author admin_hzw  *  * */public  class  Task extends TimerTask {      publicvoid  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 classTimermanager {/** * @param args*/       Public Static voidMain (string[] args) {NewTimermanager (); }        //time interval (one day)    Private StaticFinalLongPeriod_day = -* -* -* +;  PublicTimermanager () {Calendar Calendar=calendar.getinstance (); Calendar.Set(Calendar.hour_of_day,1);//1 o'clock in the morningCalendar.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 (NewDate ())) {Date= This. Addday (Date,1); } Timer Timer=NewTimer (); Task Task=NewTask (); //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     PublicDate Addday (date date,intnum) {Calendar Startdt=calendar.getinstance ();          Startdt.settime (date);          Startdt.add (Calendar.day_of_month, num); returnStartdt.gettime (); }    }  

Java timer timed daily 0 o'clock in the morning task

Related Article

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.