code: private long getTime(Date start1, Date end1, Date start2, Date end2)
     {
        long total = 0;
  
        if (start1 != null && end1 != null && start2 != null && end2 != null)
        {
           long start;
           long end;
  
           if (start1.getTime() < start2.getTime())
           {
              start = start2.getTime();
           }
           else
           {
              start = start1.getTime();
           }
  
           if (end1.getTime() < end2.getTime())
           {
              end = end1.getTime();
           }
           else
           {
              end = end2.getTime();
           }
  
           if (start < end)
           {
              total = end - start;
           }
        }
  
        return (total);
     }
docs: This method returns the length of overlapping time between two time
  ranges.
  
  @param start1 start of first range
  @param end1 end of first range
  @param start2 start start of second range
  @param end2 end of second range
  @return overlapping time in milliseconds