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 …
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.
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.
Add N business bays
importjava.time.DayOfWeek;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;importjava.util.List;importjava.util.Optional;importjava.util.function.Predicate;importjava.util.stream.Stream;publicclassBusinessDaysExamples{publicstaticvoidmain(String[] args){LocalDate today =LocalDate.of(2020,5,5);//Add one holiday for testingList<LocalDate> holidays =newArrayList<>();
holidays.add(LocalDate.of(2020,5,11));
holidays.add(LocalDate.of(2020,5,1));System.out.println(addBusinessDays(today,8,Optional.empty()));// 2020-05-15System.out.println(addBusinessDays(today,8,Optional.of(holidays)));// 2020-05-18}privatestaticLocalDateaddBusinessDays(LocalDate localDate,int days,Optional<List<LocalDate>> holidays){if(localDate ==null|| days <=0|| holidays ==null){thrownewIllegalArgumentException("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.
Subtract N business bays
importjava.time.DayOfWeek;importjava.time.LocalDate;importjava.time.temporal.ChronoUnit;importjava.util.List;importjava.util.Optional;importjava.util.function.Predicate;importjava.util.stream.Stream;publicclassBusinessDaysExamples{publicstaticvoidmain(String[] args){LocalDate today =LocalDate.of(2020,5,5);//Add one holiday for testingList<LocalDate> holidays =newArrayList<>();
holidays.add(LocalDate.of(2020,5,11));
holidays.add(LocalDate.of(2020,5,1));System.out.println(subtractBusinessDays(today,8,Optional.empty()));// 2020-04-22System.out.println(subtractBusinessDays(today,8,Optional.of(holidays)));// 2020-04-21}privatestaticLocalDatesubtractBusinessDays(LocalDate localDate,int days,Optional<List<LocalDate>> holidays){if(localDate ==null|| days <=0|| holidays ==null){thrownewIllegalArgumentException("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.
A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Comments