0

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);
    }
Stanko
  • 4,275
  • 3
  • 23
  • 51
  • Answer depens on localization as you need to take care of free days besides weekends. Do you care about holidays? – Yoda Apr 26 '15 at 10:40
  • @Yoda No, I don't care about weekends or holidays. – Stanko Apr 26 '15 at 10:47
  • Is `cal.add(Calendar.MINUTE,(this.getEstimatedDuration()%540));` what you're after? – Fox Apr 26 '15 at 10:47
  • possible duplicate of [Adding days to a date in Java](http://stackoverflow.com/questions/12087419/adding-days-to-a-date-in-java) – Basil Bourque Apr 27 '15 at 04:04
  • I gave [this Answer](http://stackoverflow.com/a/36486691/642706) to a similar Question with working source code using java.time that may be of use. In that code I was moving day-by-day, going to the first moment of the next day. You might be able to alter that logic by moving to the end of the work day instead (and then skip to start of next working day). – Basil Bourque Apr 08 '16 at 05:59

3 Answers3

2

I've found a solution using the modulo operator, explanation is in the commentary:

public TimeSpan addEstimatedToTime(Date time) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

    //first we get the days
    long workingDays =  this.getEstimatedDuration() / 540;
    //then the remaining hours and minutes
    long remainderDays = this.getEstimatedDuration() % 540;
    //then we calculate the hours
    long workingHours = remainderDays/60;
    //and the remaining are the minutes
    long workingMinutes = remainderDays % 60;

    //convert to calendar to add the estimated time to the given time
    Calendar cal = Calendar.getInstance();
    cal.setTime(time);
    cal.add(Calendar.DATE,(int) workingDays);
    cal.add(Calendar.HOUR , (int) workingHours);
    cal.add(Calendar.MINUTE, (int) workingMinutes);

    //format back to date
    String endTime = format.format(cal.getTime());
    Date plannedEndTime = format.parse(endTime);

    return new TimeSpan(time,plannedEndTime);
}
Stanko
  • 4,275
  • 3
  • 23
  • 51
0

If the estimated duration is in minutes why you don't add directly the minutes?

calendar.add(Calendar.MINUTE, yourMinutes);
antoniodvr
  • 1,259
  • 1
  • 14
  • 15
0

Use cal.add(Calendar.MINUTE, (int)taskMinutes);, after setting taskMinutes to getEstimatedDuration() and ensuring that it isn't larger than int.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • But the calendar uses 24-hours days instead of 9-hours days, that wouldn't be correct. – Stanko Apr 26 '15 at 10:45
  • So what is it that is imprecise about your method? – arcy Apr 26 '15 at 10:49
  • If I have for example a duration of 560 minutes then that would result into a estimated duration of a day and 20 minutes. My days are rounded because then I can add them to the calendar. With the rounding the 20 minutes are lost. – Stanko Apr 26 '15 at 10:54