7
7
import java .util .List ;
8
8
import java .util .Optional ;
9
9
import java .util .function .Predicate ;
10
+ import java .util .stream .Collectors ;
10
11
import java .util .stream .Stream ;
11
12
12
13
public class BusinessDaysExamples {
13
- public static void main (String [] args ) {
14
+ public static void main (final String [] args ) {
14
15
LocalDate today = LocalDate .of (2020 , 5 , 5 );
15
16
16
17
System .out .println (addBusinessDays (today , 8 , Optional .empty ()));
@@ -25,14 +26,15 @@ public static void main(String[] args) {
25
26
System .out .println (subtractBusinessDays (today , 8 , Optional .empty ()));
26
27
System .out .println (subtractBusinessDays (today , 8 , Optional .of (holidays )));
27
28
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 ( ));
30
31
}
31
32
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 ) {
33
35
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 + ")" );
36
38
}
37
39
38
40
Predicate <LocalDate > isHoliday = date -> holidays .isPresent () && holidays .get ().contains (date );
@@ -50,10 +52,11 @@ private static LocalDate addBusinessDays(LocalDate localDate, int days, Optional
50
52
return result ;
51
53
}
52
54
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 ) {
54
57
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 + ")" );
57
60
}
58
61
59
62
Predicate <LocalDate > isHoliday = date -> holidays .isPresent () && holidays .get ().contains (date );
@@ -71,21 +74,59 @@ private static LocalDate subtractBusinessDays(LocalDate localDate, int days, Opt
71
74
return result ;
72
75
}
73
76
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
76
80
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
78
82
+ "," + endDate + "," + holidays + ")" );
79
83
}
80
84
85
+ // Predicate 1 : Is a given date is holiday
81
86
Predicate <LocalDate > isHoliday = date -> holidays .isPresent () && holidays .get ().contains (date );
82
87
88
+ // Predicate 2 : Is a given date is weekday
83
89
Predicate <LocalDate > isWeekend = date -> date .getDayOfWeek () == DayOfWeek .SATURDAY
84
90
|| date .getDayOfWeek () == DayOfWeek .SUNDAY ;
85
91
92
+ // Get all days between two dates
86
93
long daysBetween = ChronoUnit .DAYS .between (startDate , endDate );
87
94
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 ;
90
131
}
91
132
}
0 commit comments