Calculate the number of business days between two dates, excluding weekends and holidays. This challenge tests your ability to work with dates, handle edge cases, and implement complex business logic.

Your method should:

  • Calculate business days between two dates (Monday through Friday only)
  • Exclude weekends (Saturday and Sunday) from the count
  • Exclude any holidays that fall on business days
  • Handle null inputs gracefully
  • Handle edge cases like same dates or end date before start date

The method signature should be:

public static Integer calculateBusinessDays(Date startDate, Date endDate, List<Date> holidays)

Examples:

// Basic calculation - Monday to Friday (same week)
Date start = Date.newInstance(2024, 1, 1); // Monday
Date end = Date.newInstance(2024, 1, 5);   // Friday
List<Date> holidays = new List<Date>();
Integer result = calculateBusinessDays(start, end, holidays);
// Returns: 4 (Tue, Wed, Thu, Fri - end date is exclusive)

// With weekend - Friday to Monday (next week)
Date start = Date.newInstance(2024, 1, 5); // Friday
Date end = Date.newInstance(2024, 1, 8);   // Monday
List<Date> holidays = new List<Date>();
Integer result = calculateBusinessDays(start, end, holidays);
// Returns: 1 (only Monday is counted, weekend is excluded)

// With holidays - Monday to Friday with Wednesday holiday
Date start = Date.newInstance(2024, 1, 1); // Monday
Date end = Date.newInstance(2024, 1, 5);   // Friday
List<Date> holidays = new List<Date>{Date.newInstance(2024, 1, 3)}; // Wednesday
Integer result = calculateBusinessDays(start, end, holidays);
// Returns: 3 (Tue, Thu, Fri - Wed excluded as holiday)

// Same date
Date start = Date.newInstance(2024, 1, 1);
Date end = Date.newInstance(2024, 1, 1);
List<Date> holidays = new List<Date>();
Integer result = calculateBusinessDays(start, end, holidays);
// Returns: 0

// End date before start date
Date start = Date.newInstance(2024, 1, 5);
Date end = Date.newInstance(2024, 1, 1);
List<Date> holidays = new List<Date>();
Integer result = calculateBusinessDays(start, end, holidays);
// Returns: 0

// Null inputs
Integer result = calculateBusinessDays(null, Date.today(), new List<Date>());
// Returns: 0
Apex Code Editor
Log in to submit your code and save your progress.

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Contest

Two ways to win a free Salesforce certification voucher:

  • The #1 ranked player on our #CodeEveryDay leaderboard by June 28th
  • Complete challenges to enter our random drawing! Each challenge you complete gives you an additional entry, increasing your chances to win.

Contest runs June 16-28. Additional prizes available for other top leaderboard positions. Track your position and compete for prizes!

Points are only calculated for questions created and completed during the contest period. If you previously won, you are not eligible to win the certification voucher again.