code: private long getTotalTime(ProjectCalendarDateRanges hours, Date startDate, Date endDate)
     {
        long total = 0;
        if (startDate.getTime() != endDate.getTime())
        {
           Date start = DateHelper.getCanonicalTime(startDate);
           Date end = DateHelper.getCanonicalTime(endDate);
  
           for (DateRange range : hours)
           {
              Date rangeStart = range.getStart();
              Date rangeEnd = range.getEnd();
              if (rangeStart != null && rangeEnd != null)
              {
                 Date canoncialRangeStart = DateHelper.getCanonicalTime(rangeStart);
                 Date canonicalRangeEnd = DateHelper.getCanonicalTime(rangeEnd);
  
                 Date startDay = DateHelper.getDayStartDate(rangeStart);
                 Date finishDay = DateHelper.getDayStartDate(rangeEnd);
  
                 //
                 // Handle the case where the end of the range is at midnight -
                 // this will show up as the start and end days not matching
                 //
                 if (startDay.getTime() != finishDay.getTime())
                 {
                    canonicalRangeEnd = DateHelper.addDays(canonicalRangeEnd, 1);
                 }
  
                 if (canoncialRangeStart.getTime() == canonicalRangeEnd.getTime() && rangeEnd.getTime() > rangeStart.getTime())
                 {
                    total += (24 * 60 * 60 * 1000);
                 }
                 else
                 {
                    total += getTime(start, end, canoncialRangeStart, canonicalRangeEnd);
                 }
              }
           }
        }
  
        return (total);
     }
docs: This method calculates the total amount of working time in a single
  day, which intersects with the supplied time range.
  
  @param hours collection of working hours in a day
  @param startDate time range start
  @param endDate time range end
  @return length of time in milliseconds