Java
LocalDate
Java 8
Weekends
Date Manipulation

How to skip weekends while adding days to LocalDate in Java 8?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Adding calendar days is easy with LocalDate.plusDays, but adding working days is a different problem. If weekends should not count, you need logic that advances the date while ignoring Saturday and Sunday. In Java 8, the clearest approach is usually a small loop over LocalDate and DayOfWeek.

Start with a Simple Business-Day Loop

The basic algorithm is: move one day at a time, count only weekdays, and stop when the requested number of working days has been added.

java
1import java.time.DayOfWeek;
2import java.time.LocalDate;
3
4public class BusinessDays {
5    public static LocalDate addBusinessDays(LocalDate start, int daysToAdd) {
6        if (daysToAdd < 0) {
7            throw new IllegalArgumentException("daysToAdd must be nonnegative");
8        }
9
10        LocalDate date = start;
11        int added = 0;
12
13        while (added < daysToAdd) {
14            date = date.plusDays(1);
15            DayOfWeek day = date.getDayOfWeek();
16            if (day != DayOfWeek.SATURDAY && day != DayOfWeek.SUNDAY) {
17                added++;
18            }
19        }
20
21        return date;
22    }
23
24    public static void main(String[] args) {
25        System.out.println(addBusinessDays(LocalDate.of(2026, 3, 6), 1));
26    }
27}

This is easy to read, easy to test, and correct for the stated rule: weekends do not count.

Be Clear About the Start-Date Rule

One subtle question is whether the start date itself should count. In the example above, the function moves forward first and then counts business days. That means a Friday plus one business day becomes Monday.

If your rule says that the current day should count when it is already a weekday, write that explicitly. Date logic becomes error-prone when assumptions about inclusiveness are left implicit.

Extend the Rule for Holidays

Weekend skipping is often only the first requirement. Real business calendars may also exclude holidays.

java
1import java.time.DayOfWeek;
2import java.time.LocalDate;
3import java.util.Set;
4
5public class BusinessDaysWithHolidays {
6    public static LocalDate addBusinessDays(LocalDate start, int daysToAdd, Set<LocalDate> holidays) {
7        LocalDate date = start;
8        int added = 0;
9
10        while (added < daysToAdd) {
11            date = date.plusDays(1);
12            DayOfWeek day = date.getDayOfWeek();
13            boolean weekend = day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY;
14            boolean holiday = holidays.contains(date);
15
16            if (!weekend && !holiday) {
17                added++;
18            }
19        }
20
21        return date;
22    }
23}

This keeps the weekend rule intact while letting a caller define a local holiday calendar.

Why a Loop Is Often Better Than a Clever Formula

It is tempting to compress the problem into arithmetic on weeks and remainders. That can work for simple cases, but the code becomes harder to reason about once you introduce holidays, custom weekends, or inclusive start rules.

The loop is explicit. It matches how the rule is described in plain language, and it remains adaptable when requirements change.

Performance is also rarely a practical issue unless you are adding very large date ranges in bulk. For normal business logic, the clarity is worth more than micro-optimizing a few date increments.

Test Boundary Cases

Date rules should be tested around Fridays, Saturdays, Sundays, month boundaries, and year boundaries.

java
System.out.println(addBusinessDays(LocalDate.of(2026, 3, 6), 1));
System.out.println(addBusinessDays(LocalDate.of(2026, 3, 7), 1));
System.out.println(addBusinessDays(LocalDate.of(2026, 12, 31), 1));

These checks confirm that the logic behaves correctly when weekends or calendar transitions are involved.

Common Pitfalls

  • Using plusDays directly and forgetting that it counts weekends.
  • Failing to define whether the start date itself counts.
  • Writing a compressed arithmetic solution that becomes fragile once holidays are introduced.
  • Forgetting to test Friday-to-Monday transitions.
  • Mixing time-zone concerns into a LocalDate problem that is purely date-based.

Summary

  • Use a small loop over LocalDate when you need to skip weekends.
  • Count only weekdays and move one day at a time.
  • Define whether the start date is inclusive or exclusive.
  • Add a holiday set when weekend rules are not enough.
  • Favor clear, testable date logic over clever but rigid formulas.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.