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
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 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.