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 !!