HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 – Calculate business days between two dates

Java 8 – Calculate business days between two dates

Learn to count the number of business days between two given dates in Java 8. The given example takes the holiday list as an optional list and uses predicates to check weekends or holidays.

Calculate number of business days

In this example, for simplicity sake, we have created two predicates to check if day is a weekend or is a holiday.

To get business days count, we first get the total days difference between two dates using ChronoUnit.DAYS.between() method.

Then we get a stream of dates from start date to end date and check each date against out two predicates.

If the day is no weekend or holiday, we consider it business day and get all count using stream aggregation method count().

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class BusinessDaysExamples 
{
	public static void main(String[] args) 
	{
		LocalDate today = LocalDate.of(2020, 5, 5);

		// Add one holiday for testing
		List<LocalDate> holidays = new ArrayList<>();
		holidays.add(LocalDate.of(2020, 5, 11));
		holidays.add(LocalDate.of(2020, 5, 1));

		System.out.println(countBusinessDaysBetween(today, today.plusDays(20), Optional.empty()));

		System.out.println(countBusinessDaysBetween(today, today.plusDays(20), Optional.of(holidays)));
	}

	private static long countBusinessDaysBetween(LocalDate startDate, LocalDate endDate,
			Optional<List<LocalDate>> holidays) 
	{
		if (startDate == null || endDate == null || holidays == null) {
			throw new IllegalArgumentException("Invalid method argument(s) to countBusinessDaysBetween(" + startDate
					+ "," + endDate + "," + holidays + ")");
		}

		Predicate<LocalDate> isHoliday = date -> holidays.isPresent() ? holidays.get().contains(date) : false;

		Predicate<LocalDate> isWeekend = date -> date.getDayOfWeek() == DayOfWeek.SATURDAY
				|| date.getDayOfWeek() == DayOfWeek.SUNDAY;

		long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);

		long businessDays = Stream.iterate(startDate, date -> date.plusDays(1)).limit(daysBetween)
				.filter(isHoliday.or(isWeekend).negate()).count();
		return businessDays;
	}
}

Program output.

14
13

Drop me your questions related to Calculate the number of weekdays between two dates in Java.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)