title: Calculate number of business days between two dates
body: <p>I have requirement to calculate number of business days between two given dates<br>
  I have the list of holidays as an Array list provided by the user.<br>
  So I can investigate each and every day between the dates and check if its weekday and not federal holiday like the code I provided below (which is working fine)</p>
  
  <p>But this is very expensive, lets say 12 federal holidays and each day I will   have to check its not a weekend,<br>
  so if I need to count between 5 years it will take 365 * 5 * 12  its 21,000  iterations! its crazy (not even including the calculation for business day)<br>
  Is there a better way?</p>
  
  <pre><code>package test;
  
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Arrays;
  
  import java.util.Calendar;
  import java.util.Date;
  import java.util.List;
  
  import org.apache.commons.lang3.time.DateUtils;
  
  public class TestDiff {
  
      public static void main(String[] args) throws ParseException {
          DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
          // add 4 years as an example
          Date fromDate = formatter.parse("11/06/2017"),toDate = formatter.parse("11/29/2017");// DateUtils.addDays(fromDate,365 * 4);
          int numberOfDaysCount=0;
          int daysBetween  = daysBetween(fromDate,toDate);
          Date caurDate = fromDate;
  
          for(int i=0;i&lt;=daysBetween ; i++ ) {
              if(isWeekDay(caurDate) &amp;&amp; !isFederalHoliday(caurDate) )
                  numberOfDaysCount++;
              caurDate = DateUtils.addDays(caurDate,1); // add one day
          }
          System.out.println("number of business days between "+fromDate+" and "+toDate+" is: "+numberOfDaysCount);
      }
  
     private static boolean isWeekDay(Date caurDate) {
               Calendar c = Calendar.getInstance();
               c.setTime(caurDate);
                int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
                return dayOfWeek!= Calendar.SATURDAY &amp;&amp; dayOfWeek!= Calendar.SUNDAY ;
      }
  
          private static boolean isFederalHoliday(Date caurDate) throws ParseException {
              DateFormat formatter = new SimpleDateFormat("MM/dd/yy");    //list will come from dao.getFederalHoliday();
                  List&lt;Date&gt; federalHolidays =  Arrays.asList(formatter.parse("01/02/2017"),formatter.parse("01/16/2017"),formatter.parse("02/20/2017"),formatter.parse("05/29/2017"),formatter.parse("07/04/2017"),formatter.parse("09/04/2017"),formatter.parse("10/09/2017"),formatter.parse("07/04/2017"),formatter.parse("11/10/2017"),formatter.parse("11/23/2017"),formatter.parse("12/25/2017"));
                  for (Date holiday : federalHolidays) {
                      if(DateUtils.isSameDay(caurDate,holiday)) //using Apache commons-lang 
                          return true;
                  }
                  return false;
      }
  
          public static int daysBetween(Date d1, Date d2){
               return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
       }
  
  }
  </code></pre>