I am executing a job every 16 minutes within an hour (3 Runs). The job will run at 16 past, 32 past and 48 past the hour. It will not spill over into the next hour.
Here is a sample of the runs for a two hour window:
Run 1:
Thu Jul 04 06:16:00 EDT 2013
Run 2:
Thu Jul 04 06:32:00 EDT 2013
Run 3:
Thu Jul 04 06:48:00 EDT 2013
Run 4:
Thu Jul 04 07:16:00 EDT 2013
Run 5:
Thu Jul 04 07:32:00 EDT 2013
Run 6:
Thu Jul 04 07:48:00 EDT 2013
My problem is that I need to build some targets for each job (not important), but ultimately I decided upon determining how many jobs would run for the day and then setting a target for each job.
I am working on the logic to determine how many times the job will run per day given a variable start time. I tried doing this with some calculations, but then ultimately relied on the this piece of code that uses a calendar to figure out how many runs are left in the day:
public int getRunsLeftInDay(int runIntervalMinutes, int offset){
/*
Offset is when the job starts
Currently runIntervalMinutes = 16 && offset = 16
So start 16 minutes after the hour and run on 16 minute interval
*/
/*
This method is tested returns proper next run date, basically gives
the next 16 past || 32 past || 48 past time that will occur
*/
Date nextRunDt = this.getNextRun(runIntervalMinutes, offset);
//Determine last run per hour
int lastRunMinute = (60/runIntervalMinutes) * runIntervalMinutes;
int runCount = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(nextRunDt);
int currentDay = cal.get(Calendar.DAY_OF_YEAR);
while(cal.get(Calendar.DAY_OF_YEAR) == currentDay){
runCount++; //count first run since calendar is set to nextRunDt
if(cal.get(Calendar.MINUTE) == lastRunMinute){
cal.add(Calendar.HOUR_OF_DAY, 1);
cal.set(Calendar.MINUTE, offset);
}else{
cal.add(Calendar.MINUTE, runIntervalMinutes);
}
}
return runCount;
}
Currently my code is returning the correct amount of runs, but I would like to explore if there is a better way of determining this value (through some Date calculations).
So basically I have two questions:
1. Is this method of finding the amount of runs per day sufficient?
2. How would I do this using a date/time calculations?