Question

I am trying to run a certain task every Friday in a week at any time. So I decided to use ScheduledExecutorService for this but so far I have seen examples which shows how to run task every few minutes.

Below is my code which I adopted to run every day at 5 AM in the morning. How do I use this to run task every Friday in a week at any time?

public static void main(String[] args) {

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
    Date aDate = new Date();
    Calendar with = Calendar.getInstance();
    with.setTime(aDate);

    int hour = with.get(Calendar.HOUR_OF_DAY);
    int intDelayInHour = hour < 5 ? 5 - hour : 24 - (hour - 5);

    System.out.println("Current Hour: " + hour);
    System.out.println("Comuted Delay for next 5 AM: " + intDelayInHour);

    scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
            try {
                getDataFromDatabase();
            } catch (Exception ex) {
                ex.printStackTrace(); // or loggger would be better
            }
        }
    }, intDelayInHour, 24, TimeUnit.HOURS);
}

protected static void getDataFromDatabase() {
    // TODO Auto-generated method stub

}

Can anyone provide an example how would I do this?

No correct solution

OTHER TIPS

You would have to check what day it is today.

Then set the delay to the next Friday (lets say it is Tuesday, then set a 3 day delay, or use hours if you want to set it at a different time).

And then use a 7 day period (or equivallent in hours).

EDIT:

As requested you can do something like.

Map<Integer, Integer> dayToDelay = new HashMap<Integer, Integer>()
dayToDelay.put(Calendar.FRIDAY, 0);
dayToDelay.put(Calendar.SATURDAY, 6);
dayToDelay.put(Calendar.SUNDAY, 5);
dayToDelay.put(Calendar.MONDAY, 4);
dayToDelay.put(Calendar.TUESDAY, 3);
dayToDelay.put(Calendar.WEDNESDAY, 2);
dayToDelay.put(Calendar.THURSDAY, 1);
int dayOfWeek = with.get(DAY_OF_WEEK);
int delayInDays = dayToDelay.get(dayOfWeek);

scheduler.scheduleAtFixedRate(new Runnable() {
    public void run() {
        try {
            getDataFromDatabase();
        } catch (Exception ex) {
            ex.printStackTrace(); // or loggger would be better
        }
    }
}, delayInDays, 7, TimeUnit.DAYS);

That should run the task every Friday at the time this is executed.

TO RUN PROGRAM EVERY MONDAY AT 11 AM

public static void main(String args[]){
    new WeeklyReportService();
}

WeeklyReportService.java

public class WeeklyReportService{

public WeeklyReportService(){
    this.startScheduler();
}

private void startScheduler(){
    Calendar with = Calendar.getInstance();
    Map<Integer, Integer> dayToDelay = new HashMap<Integer, Integer>();
            dayToDelay.put(Calendar.FRIDAY, 2);
            dayToDelay.put(Calendar.SATURDAY, 1);
            dayToDelay.put(Calendar.SUNDAY, 0);
            dayToDelay.put(Calendar.MONDAY, 6);
            dayToDelay.put(Calendar.TUESDAY, 5);
            dayToDelay.put(Calendar.WEDNESDAY, 4);
            dayToDelay.put(Calendar.THURSDAY, 3);
            int dayOfWeek = with.get(Calendar.DAY_OF_WEEK);
            int hour = with.get(Calendar.HOUR_OF_DAY);
            int delayInDays = dayToDelay.get(dayOfWeek);
            int delayInHours = 0;
            if(delayInDays == 6 && hour<11){
                delayInHours = 11 - hour;
            }else{
                delayInHours = delayInDays*24+((24-hour)+11);
            }
     ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);            
     scheduler.scheduleAtFixedRate(new WeeklyTask(), delayInHours,
                                   179, TimeUnit.HOURS);
}

WeeklyTask.java

Public class WeeklyTask implements Runnable {


@Override
public void run() {
    System.out.println("start of weekly report");
    /*Your Program to run*/
    System.out.println("end of weekly report");
}

}

Hope this helps! You can apply same to any day. In your case, for Friday 11 AM it will be

            dayToDelay.put(Calendar.FRIDAY, 6);
            dayToDelay.put(Calendar.SATURDAY, 5);
            dayToDelay.put(Calendar.SUNDAY, 4);
            dayToDelay.put(Calendar.MONDAY, 3);
            dayToDelay.put(Calendar.TUESDAY, 2);
            dayToDelay.put(Calendar.WEDNESDAY, 1);
            dayToDelay.put(Calendar.THURSDAY, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top