I'm working on a basic project that manages tasks. Working days are from 8:00 to 17:00. A task has a start time and an estimated duration. This estimated duration is in minutes. I want to calculate the end time.
I've figured out how to add days to the start time but it is far from precise. I divide the estimated duration by 540 (= 9 hours) so I know how many days there approximately are. But I want to be able to calculate the end date precisly.
I can't just can't add the minutes to the calendar because the calendar uses 24-hours days instead of 9-hours days. For example if the start time is 2015-04-26 16:00:00.0 and I add 180 minutes (3 hours) then the end time would be 2015-04-27 10:00:00.0. How would this be done? This is what I have so far:
public TimeSpan calculateEstimatedDuration() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
//divide by 540 because there are 9 hours in a work day
long workingDays = Math.round((this.getEstimatedDuration()/540));
//convert to calendar to add the estimated working days to the start date
Date startDate = this.getPlannedStartTime();
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.add(Calendar.DATE,(int) workingDays);
String endTime = format.format(cal.getTime());
Date PlannedEndTime = format.parse(endTime);
return new TimeSpan(this.getPlannedStartTime(),PlannedEndTime);
}