HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 – Add or subtract N business days to date

Java 8 – Add or subtract N business days to date

Learn to add or subtract a given number of business days to LocalDate instance in Java 8. The given example takes the holiday list as well into consideration.

1. Adding business days

To add business days to a LocalDate instance, we need to add 1 day to LocalDate and then check if it is no a weekend or holiday.

If the day is either weekend or holiday, we add again 1 day to date and decrease the days by 1.

If the day is neither weekend nor holiday, we add the day again without changing the days counter.

Once the counter reaches zero, we have the LocalDate instance after N business days.

If we have to consider holidays as well then at steps 2 and 3, check the date to be weekend AND holiday, both.

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
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(addBusinessDays(today, 8, Optional.empty()));		// 2020-05-15
		System.out.println(addBusinessDays(today, 8, Optional.of(holidays)));	// 2020-05-18
	}
	
	private static LocalDate addBusinessDays(LocalDate localDate, int days, 
                                             Optional<List<LocalDate>> holidays)
	{
		if(localDate == null || days <= 0 || holidays == null)
		{
			throw new IllegalArgumentException("Invalid method argument(s) "
						+ "to addBusinessDays("+localDate+","+days+","+holidays+")");
		}
		
		Predicate<LocalDate> isHoliday = 
			date -> holidays.isPresent() ? holidays.get().contains(date) : false;
		
		Predicate<LocalDate> isWeekend = date 
			-> date.getDayOfWeek() == DayOfWeek.SATURDAY 
				|| date.getDayOfWeek() == DayOfWeek.SUNDAY;
		
		LocalDate result = localDate;
		while (days > 0) {
	        result = result.plusDays(1);
	        if (isHoliday.or(isWeekend).negate().test(result)) {
	            days--;
	        }
	    }
		return result;
	}
}

2. Subtract business days

To subtract business days to a LocalDate instance, we need to subtract 1 day to LocalDate and then check if it is no a weekend or holiday.

If the day is either weekend or holiday, we add again 1 day to date and decrease the days by 1.

If the day is neither weekend nor holiday, we add the day again without changing the days counter.

Once the counter reaches zero, we have the LocalDate instance after N business days.

If we have to consider holidays as well then at steps 2 and 3, check the date to be weekend AND holiday, both.

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
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(subtractBusinessDays(today, 8, Optional.empty()));		// 2020-04-22
		System.out.println(subtractBusinessDays(today, 8, Optional.of(holidays)));	// 2020-04-21
	}
	
	private static LocalDate subtractBusinessDays(LocalDate localDate, int days, 
                                                  Optional<List<LocalDate>> holidays)
	{
		if(localDate == null || days <= 0 || holidays == null)
		{
			throw new IllegalArgumentException("Invalid method argument(s) "
					+ "to subtractBusinessDays("+localDate+","+days+","+holidays+")");
		}
		
		Predicate<LocalDate> isHoliday = 
			date -> holidays.isPresent() ? holidays.get().contains(date) : false;
		
		Predicate<LocalDate> isWeekend = 
			date -> date.getDayOfWeek() == DayOfWeek.SATURDAY 
				|| date.getDayOfWeek() == DayOfWeek.SUNDAY;
		
		LocalDate result = localDate;
		while (days >= 0) {
	        result = result.minusDays(1);
	        if (isHoliday.or(isWeekend).negate().test(result)) {
	            days--;
	        }
	    }
		return result;
	}
}

Drop me your questions related to adding or subtracting business days in Java 8.

Happy Learning !!

Sourcecode Download

Was this post helpful?

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

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

  • Sealed Classes and Interfaces