Skip to content

Commit 7354bc5

Browse files
Update BusinessDaysExamples.java
1 parent def248a commit 7354bc5

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

src/com/howtodoinjava/core/datetime/BusinessDaysExamples.java

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import java.util.List;
88
import java.util.Optional;
99
import java.util.function.Predicate;
10+
import java.util.stream.Collectors;
1011
import java.util.stream.Stream;
1112

1213
public class BusinessDaysExamples {
13-
public static void main(String[] args) {
14+
public static void main(final String[] args) {
1415
LocalDate today = LocalDate.of(2020, 5, 5);
1516

1617
System.out.println(addBusinessDays(today, 8, Optional.empty()));
@@ -25,14 +26,15 @@ public static void main(String[] args) {
2526
System.out.println(subtractBusinessDays(today, 8, Optional.empty()));
2627
System.out.println(subtractBusinessDays(today, 8, Optional.of(holidays)));
2728

28-
System.out.println(countBusinessDaysBetween(today, today.plusDays(20), Optional.empty()));
29-
System.out.println(countBusinessDaysBetween(today, today.plusDays(20), Optional.of(holidays)));
29+
System.out.println(countBusinessDaysBetween_Java8(today, today.plusDays(20), Optional.empty()).size());
30+
System.out.println(countBusinessDaysBetween_Java9(today, today.plusDays(20), Optional.of(holidays)).size());
3031
}
3132

32-
private static LocalDate addBusinessDays(LocalDate localDate, int days, Optional<List<LocalDate>> holidays) {
33+
private static LocalDate addBusinessDays(final LocalDate localDate, int days,
34+
final Optional<List<LocalDate>> holidays) {
3335
if (localDate == null || days <= 0) {
34-
throw new IllegalArgumentException("Invalid method argument(s) " + "to addBusinessDays(" + localDate + ","
35-
+ days + "," + holidays + ")");
36+
throw new IllegalArgumentException(
37+
"Invalid method argument(s) to addBusinessDays(" + localDate + "," + days + "," + holidays + ")");
3638
}
3739

3840
Predicate<LocalDate> isHoliday = date -> holidays.isPresent() && holidays.get().contains(date);
@@ -50,10 +52,11 @@ private static LocalDate addBusinessDays(LocalDate localDate, int days, Optional
5052
return result;
5153
}
5254

53-
private static LocalDate subtractBusinessDays(LocalDate localDate, int days, Optional<List<LocalDate>> holidays) {
55+
private static LocalDate subtractBusinessDays(final LocalDate localDate, int days,
56+
final Optional<List<LocalDate>> holidays) {
5457
if (localDate == null || days <= 0) {
55-
throw new IllegalArgumentException("Invalid method argument(s) " + "to subtractBusinessDays(" + localDate
56-
+ "," + days + "," + holidays + ")");
58+
throw new IllegalArgumentException(
59+
"Invalid method argument(s) to addBusinessDays(" + localDate + "," + days + "," + holidays + ")");
5760
}
5861

5962
Predicate<LocalDate> isHoliday = date -> holidays.isPresent() && holidays.get().contains(date);
@@ -71,21 +74,59 @@ private static LocalDate subtractBusinessDays(LocalDate localDate, int days, Opt
7174
return result;
7275
}
7376

74-
private static long countBusinessDaysBetween(LocalDate startDate, LocalDate endDate,
75-
Optional<List<LocalDate>> holidays) {
77+
private static List<LocalDate> countBusinessDaysBetween_Java8(final LocalDate startDate, final LocalDate endDate,
78+
final Optional<List<LocalDate>> holidays) {
79+
// Validate method arguments
7680
if (startDate == null || endDate == null) {
77-
throw new IllegalArgumentException("Invalid method argument(s) to countBusinessDaysBetween(" + startDate
81+
throw new IllegalArgumentException("Invalid method argument(s) to countBusinessDaysBetween (" + startDate
7882
+ "," + endDate + "," + holidays + ")");
7983
}
8084

85+
// Predicate 1 : Is a given date is holiday
8186
Predicate<LocalDate> isHoliday = date -> holidays.isPresent() && holidays.get().contains(date);
8287

88+
// Predicate 2 : Is a given date is weekday
8389
Predicate<LocalDate> isWeekend = date -> date.getDayOfWeek() == DayOfWeek.SATURDAY
8490
|| date.getDayOfWeek() == DayOfWeek.SUNDAY;
8591

92+
// Get all days between two dates
8693
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
8794

88-
return Stream.iterate(startDate, date -> date.plusDays(1)).limit(daysBetween)
89-
.filter(isHoliday.or(isWeekend).negate()).count();
95+
// List<LocalDate> businessDays = startDate.datesUntil(endDate)
96+
// .filter(isWeekend.or(isHoliday))
97+
// .collect(Collectors.toList());
98+
99+
// Iterate over stream of all dates and check each day against any weekday or
100+
// holiday
101+
return Stream.iterate(startDate, date -> date.plusDays(1))
102+
.limit(daysBetween)
103+
.filter(isHoliday.or(isWeekend).negate())
104+
.collect(Collectors.toList());
105+
}
106+
107+
private static List<LocalDate> countBusinessDaysBetween_Java9(final LocalDate startDate,
108+
final LocalDate endDate,
109+
final Optional<List<LocalDate>> holidays) {
110+
// Validate method arguments
111+
if (startDate == null || endDate == null) {
112+
throw new IllegalArgumentException("Invalid method argument(s) to countBusinessDaysBetween (" + startDate
113+
+ "," + endDate + "," + holidays + ")");
114+
}
115+
116+
// Predicate 1 : Is a given date is holiday
117+
Predicate<LocalDate> isHoliday = date -> holidays.isPresent()
118+
&& holidays.get().contains(date);
119+
120+
// Predicate 2 : Is a given date is weekday
121+
Predicate<LocalDate> isWeekend = date -> date.getDayOfWeek() == DayOfWeek.SATURDAY
122+
|| date.getDayOfWeek() == DayOfWeek.SUNDAY;
123+
124+
// Iterate over stream of all dates and check each day against any weekday or
125+
// holiday
126+
List<LocalDate> businessDays = startDate.datesUntil(endDate)
127+
.filter(isWeekend.or(isHoliday).negate())
128+
.collect(Collectors.toList());
129+
130+
return businessDays;
90131
}
91132
}

0 commit comments

Comments
 (0)