code: public static Duration getVariance(Task task, Date date1, Date date2, TimeUnit format)
     {
        Duration variance = null;
  
        if (date1 != null && date2 != null)
        {
           ProjectCalendar calendar = task.getEffectiveCalendar();
           if (calendar != null)
           {
              variance = calendar.getWork(date1, date2, format);
           }
        }
  
        if (variance == null)
        {
           variance = Duration.getInstance(0, format);
        }
  
        return (variance);
     }
docs: This utility method calculates the difference in working
  time between two dates, given the context of a task.
  
  @param task parent task
  @param date1 first date
  @param date2 second date
  @param format required format for the resulting duration
  @return difference in working time between the two dates