Learn to add or subtract a given number of business days to new date times classes in Java such as LocalDate, LocalDateTime and ZonedDateTime. The given example takes the holidays list as well into consideration.
Read More: Add or Subtract Days to/from Date
1. Adding Business Days
It uses two Predicate instances isHoliday
and isWeekend
. We repeatedly increment the LocalDate instance by 1 using the method plusDays()
and check if the new date satisfies anyone of the given predicates.
If the new date is either weekend or holiday, we do not decrease the days counter. If the date is a business day, we decrease the day counter.
Continue adding days until the day counter reaches to 0
. Once the counter reaches zero, we have the LocalDate
instance after N business days.
The given example can be used for LocalDateTime and ZonedDateTime classes as well.
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. Subtracting Business Days
To subtract the business days, we have to follow the exact logic written above. Except, we will decrement the LocalDate instance by 1 using the minusDays()
method, every time.
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.
Happy Learning !!
Comments