title: How do I ignore weekends using the Java calendar?
body: <p>I'm trying to get the number of minutes between two particular time instances by ignoring weekends. This is what I've done. </p>
  
  <pre><code>public static final List&lt;Integer&gt; NON_WORKING_DAYS;
  static {
      List&lt;Integer&gt; nonWorkingDays = new ArrayList&lt;Integer&gt;();
      nonWorkingDays.add(Calendar.SATURDAY);
      nonWorkingDays.add(Calendar.SUNDAY);
      NON_WORKING_DAYS = Collections.unmodifiableList(nonWorkingDays);
  }
  
  public static int getMinsBetween(Date d1, Date d2, boolean onlyBusinessDays)
  {
      int minsBetween = (int)((d2.getTime() - d1.getTime()) / (1000 * 60));
      int minsToSubtract = 0;
      if(onlyBusinessDays){
          Calendar dateToCheck = Calendar.getInstance();
          dateToCheck.setTime(d1);
          Calendar dateToCompare = Calendar.getInstance();
          dateToCompare.setTime(d2);
  
  
          //moving the first day of the week to Tues so that a Sat, sun and mon fall in the same week, easy to adjust dates
          dateToCheck.setFirstDayOfWeek(Calendar.TUESDAY);
          dateToCompare.setFirstDayOfWeek(Calendar.TUESDAY);
  
          //moving the dates out of weekends
          if(!isBusinessDay(dateToCheck, NON_WORKING_DAYS)){
              dateToCheck.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
              dateToCheck.set(Calendar.HOUR, 0);
              dateToCheck.set(Calendar.MINUTE, 0);
              dateToCheck.set(Calendar.SECOND, 0);
              dateToCheck.set(Calendar.MILLISECOND, 0);
          }
  
          if(!isBusinessDay(dateToCompare, NON_WORKING_DAYS)){
              dateToCompare.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
              dateToCompare.set(Calendar.HOUR, 0);
              dateToCompare.set(Calendar.MINUTE, 0);
              dateToCompare.set(Calendar.SECOND, 0);
              dateToCompare.set(Calendar.MILLISECOND, 0);
          }
  
          for(; dateToCheck.getTimeInMillis() &lt; dateToCompare.getTimeInMillis() ; dateToCheck.add(Calendar.DAY_OF_MONTH, 1)){
              if(isBusinessDay(dateToCheck, NON_WORKING_DAYS)){
                  minsToSubtract = minsToSubtract + 1440;
              }
          }
      minsBetween = minsBetween - minsToSubtract; 
      }
      return minsBetween;
  }
  
  
  private static boolean isBusinessDay(Calendar dateToCheck, List&lt;Integer&gt; daysToExclude){
      for(Integer dayToExclude : daysToExclude){
          if(dayToExclude != null &amp;&amp; dayToExclude == dateToCheck.get(Calendar.DAY_OF_WEEK)) {
              return true; 
          }
          else continue;
      }
      return false;
  }
  </code></pre>
  
  <p>Can someone tell me if my logic is correct and if not how to do this? I'm not too sure how this code would behave when the month changes over the weekend. </p>
  
  <p>Expected output for some test cases:</p>
  
  <ol>
  <li>Friday 6 pm, Monday 6 am - should return 12 hours</li>
  <li>Saturday 12 pm, Sunday 12 pm - should return 0 hours</li>
  <li>Saturday 12 pm, Monday 6 am - should return 6 hours</li>
  </ol>