Add Business Days to a Date in Java
Problem
Business days are the working days of the week, usually Monday to Friday, excluding weekends and public holidays.
Given an order date, find the date that is five business days later.
Java Program
import java.time.DayOfWeek;
import java.time.LocalDate;
public class AddBusinessDays {
static LocalDate addBusinessDays(LocalDate start, int days) {
LocalDate d = start;
int added = 0;
while (added < days) {
d = d.plusDays(1);
DayOfWeek dow = d.getDayOfWeek();
if (dow != DayOfWeek.SATURDAY && dow != DayOfWeek.SUNDAY) added++; // skip weekends
}
return d;
}
public static void main(String[] args) {
LocalDate orderDate = LocalDate.of(2026, 3, 12); // a Thursday
System.out.println("Order placed: " + orderDate + " (" + orderDate.getDayOfWeek() + ")");
LocalDate due = addBusinessDays(orderDate, 5);
System.out.println("Delivery by: " + due + " (" + due.getDayOfWeek() + ")");
}
}Output
Core Logic
Step forward one day at a time and count only the days that aren't Saturday or Sunday.
- 1
dstarts at the order date and moves forward withplusDays(1). - 2Each new day is counted only if its
DayOfWeekisn't a weekend day. - 3The loop stops after five business days have been counted.
- 4The result is printed with its weekday.
Key Point: Start counting from the day after the order date — counting the order date itself is a common off-by-one error.
Why: The loop visits roughly 7/5 × n calendar days to find n business days.
Key Concepts
Approach 2: Skipping Public Holidays
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Set;
public class AddBusinessDaysHolidays {
public static void main(String[] args) {
Set<LocalDate> holidays = Set.of(LocalDate.of(2026, 3, 16)); // a public holiday on Monday
LocalDate d = LocalDate.of(2026, 3, 12);
int added = 0;
while (added < 5) {
d = d.plusDays(1);
boolean weekend = d.getDayOfWeek() == DayOfWeek.SATURDAY || d.getDayOfWeek() == DayOfWeek.SUNDAY;
if (!weekend && !holidays.contains(d)) added++;
}
System.out.println("5 business days after 2026-03-12, skipping the holiday: " + d);
}
}
Output
Core Logic
A set of holiday dates extends the same loop: a day counts only if it's a weekday and not in the set.
- 1
Set.of(...)holds the public holidays. - 2Each day is checked against both the weekend rule and the holiday set.
- 3The loop otherwise works exactly like the weekend-only version.
Key Point: Holiday lists vary by country and year, so real systems load them from configuration rather than code.
Why: Each day costs an O(1) set lookup, and the set holds h holidays.
Key Concepts
Interview Questions
How would you count business days between two dates?
Loop from the start to the end date and count weekdays, or compute whole weeks × 5 plus the weekdays in the remaining partial week.
What if an order is placed on a weekend?
Decide whether counting starts on the next Monday; the loop above already skips weekend days, so it works either way.