code: private long getTime(Date start, Date end, long target, boolean after)
     {
        long total = 0;
        if (start != null && end != null)
        {
           Date startTime = DateHelper.getCanonicalTime(start);
           Date endTime = DateHelper.getCanonicalTime(end);
  
           Date startDay = DateHelper.getDayStartDate(start);
           Date finishDay = DateHelper.getDayStartDate(end);
  
           //
           // 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())
           {
              endTime = DateHelper.addDays(endTime, 1);
           }
  
           int diff = DateHelper.compare(startTime, endTime, target);
           if (diff == 0)
           {
              if (after == true)
              {
                 total = (endTime.getTime() - target);
              }
              else
              {
                 total = (target - startTime.getTime());
              }
           }
           else
           {
              if ((after == true && diff < 0) || (after == false && diff > 0))
              {
                 total = (endTime.getTime() - startTime.getTime());
              }
           }
        }
        return (total);
     }
docs: Calculates how much of a time range is before or after a
  target intersection point.
  
  @param start time range start
  @param end time range end
  @param target target intersection point
  @param after true if time after target required, false for time before
  @return length of time in milliseconds