java
39 lines · 7 steps
Counting business days between two dates in Java
A static utility counts weekdays between two dates, subtracting weekday holidays, using a whole-week shortcut for speed.
Explained by
highlit
1public final class BusinessDays {
2
3 private BusinessDays() {
4 }
5
6 public static long between(LocalDate start, LocalDate end, Set<LocalDate> holidays) {
7 if (start.isAfter(end)) {
8 return -between(end, start, holidays);
9 }
10
11 long totalDays = ChronoUnit.DAYS.between(start, end);
12 long fullWeeks = totalDays / 7;
13 long businessDays = fullWeeks * 5;
14
15 LocalDate cursor = start.plusDays(fullWeeks * 7);
16 while (cursor.isBefore(end)) {
17 if (!isWeekend(cursor)) {
18 businessDays++;
19 }
20 cursor = cursor.plusDays(1);
21 }
22
23 long holidayCount = holidays.stream()
24 .filter(h -> !h.isBefore(start) && h.isBefore(end))
25 .filter(h -> !isWeekend(h))
26 .count();
27
28 return businessDays - holidayCount;
29 }
30
31 public static long between(LocalDate start, LocalDate end) {
32 return between(start, end, Collections.emptySet());
33 }
34
35 private static boolean isWeekend(LocalDate date) {
36 DayOfWeek day = date.getDayOfWeek();
37 return day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY;
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Decomposing a range into full weeks plus a remainder avoids iterating every single day.
- 2Handling the reversed-argument case by recursing with a negated result keeps the core logic one-directional.
- 3A private constructor signals a class meant only for static helper methods.
Related explainers
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
ruby
class KeyTransformer def self.camelize(data) new.camelize(data) end
Recursively camelizing nested Ruby data
recursion
data-transformation
pattern-matching
Intermediate
7 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
java
@Entity @Table(name = "orders") @SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?") @Where(clause = "deleted = false")
Soft deletes with Hibernate in Spring
soft-delete
jpa
hibernate
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/counting-business-days-between-two-dates-in-java-explained-java-9134/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.