Simple Scheduler And Processor in java




You are here : HomeCore Java Tutorials /


Program to Schedule to run every Sunday in midnight in java >


import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;



public class SchedulerSundayNightRun {

    public static void main(String[] args) {
          Timer timernew Timer();
          Calendar date = Calendar.getInstance();

          // Schedule to run every Sunday in midnight
          date.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
          date.set(Calendar.HOUR, 0);
          date.set(Calendar.MINUTE, 0);
          date.set(Calendar.SECOND, 0);
          date.set(Calendar.MILLISECOND, 0);
  
          timer.schedule(new ProcessingClass(), date.getTime(),
                       1000 * 60 * 60 * 24 * 7 // 1000 * 60 * 60 * 24 * 7 (1 week time)
                      
          );

    }
}


class ProcessingClass extends TimerTask {

    public void run() {
          System.out.println("The task is called at time = "new Date());
          // Business logic ...
    }

}

Schedule to run every 51st minute of hour in java >

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;



public class SchedulerSundayNightRun {

    public static void main(String[] args) {
          Timer timernew Timer();
          Calendar date = Calendar.getInstance();

          // will run
          date.set(Calendar.MINUTE, 51);
          // If at present 40m > than start at 51th min
          // or if 51th min >than start running

          timer.schedule(new ProcessingClass(), date.getTime(),
                       1000 // Pass 1000 to run after 1 second
          );

    }
}


class ProcessingClass extends TimerTask {

    public void run() {
          System.out.println("The task is called at time = "new Date());
          // Business logic ...
    }

}

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>

How to convert ArrayList to HashSet and Set to Array in Java examples

convert Array to Set and Set to array in Java

How to convert Array to List and ArrayList to array in Java


Collection - List, Set and Map all properties in tabular form



Collection - List, Set and Map all properties in tabular form



eEdit
Must read for you :