Merge pull request #15647 from sam-gardner/BAEL-7382-Calculate-number-of-weekdays-between-two-dates
BAEL-7382 Add code for calculating weekdays between two dates
This commit is contained in:
commit
7ff914a1b3
|
@ -0,0 +1,6 @@
|
||||||
|
## Core Date Operations (Part 4)
|
||||||
|
This module contains articles about date operations in Java.
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-date-operations-4</artifactId>
|
||||||
|
<name>core-java-date-operations-4</name>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>11</source>
|
||||||
|
<target>11</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>joda-time</groupId>
|
||||||
|
<artifactId>joda-time</artifactId>
|
||||||
|
<version>${joda-time.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<joda-time.version>2.12.5</joda-time.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.baeldung.calculateweekdays;
|
||||||
|
|
||||||
|
import java.time.DayOfWeek;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.time.temporal.TemporalAdjusters;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class CalculateWeekdays {
|
||||||
|
|
||||||
|
public long getWorkingDaysWithStream(LocalDate start, LocalDate end){
|
||||||
|
return start.datesUntil(end)
|
||||||
|
.map(LocalDate::getDayOfWeek)
|
||||||
|
.filter(day -> !Arrays.asList(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY).contains(day))
|
||||||
|
.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getWorkingDaysWithoutStream(LocalDate start, LocalDate end) {
|
||||||
|
boolean startOnWeekend = false;
|
||||||
|
|
||||||
|
// If starting at the weekend, move to following Monday
|
||||||
|
if(start.getDayOfWeek().getValue() > 5){
|
||||||
|
start = start.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
|
||||||
|
startOnWeekend = true;
|
||||||
|
}
|
||||||
|
boolean endOnWeekend = false;
|
||||||
|
// If ending at the weekend, move to previous Friday
|
||||||
|
if(end.getDayOfWeek().getValue() > 5){
|
||||||
|
end = end.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
|
||||||
|
endOnWeekend = true;
|
||||||
|
}
|
||||||
|
// Cover case where starting on Saturday and ending following Sunday
|
||||||
|
if(start.isAfter(end)){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Get total weeks
|
||||||
|
long weeks = ChronoUnit.WEEKS.between(start, end);
|
||||||
|
|
||||||
|
long addValue = startOnWeekend || endOnWeekend ? 1 : 0;
|
||||||
|
|
||||||
|
// Add on days that did not make up a full week
|
||||||
|
return ( weeks * 5 ) + ( end.getDayOfWeek().getValue() - start.getDayOfWeek().getValue() ) + addValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
package com.baeldung.calculateweekdays;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.assertEquals;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class CalculateWeekdaysUnitTest {
|
||||||
|
|
||||||
|
// Start Saturday end following Sunday (answer is 0)
|
||||||
|
LocalDate startTomorrow = LocalDate.of(2023, 12, 2);
|
||||||
|
LocalDate endTomorrow = LocalDate.of(2023, 12, 3);
|
||||||
|
|
||||||
|
// Three week gap with midweek start and finish (answer is 17)
|
||||||
|
LocalDate startThreeWeeks = LocalDate.of(2023, 11, 28);
|
||||||
|
LocalDate endThreeWeeks = LocalDate.of(2023, 12, 21);
|
||||||
|
|
||||||
|
// Three week gap with midweek start and weekend finish (answer is 17)
|
||||||
|
LocalDate startThreeWeeks2 = LocalDate.of(2023, 11, 6);
|
||||||
|
LocalDate endThreeWeeks2 = LocalDate.of(2023, 12, 30);
|
||||||
|
|
||||||
|
// Week gap start and end on weekend (answer is 40)
|
||||||
|
LocalDate startThreeWeeksWeekend = LocalDate.of(2023, 12, 2);
|
||||||
|
LocalDate endThreeWeeksWeekend = LocalDate.of(2023, 12, 9);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTwoDaysOnSameWeekend_whenUsingStreams_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithStream(startTomorrow, endTomorrow);
|
||||||
|
assertEquals(0, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenTwoDaysOnSameWeekend_whenUsingMaths_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithoutStream(startTomorrow, endTomorrow);
|
||||||
|
assertEquals(0, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAThreeWeekGapMidweekDates_whenUsingStreams_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithStream(startThreeWeeks, endThreeWeeks);
|
||||||
|
assertEquals(17, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAThreeWeekGapMidweekDates_whenUsingMaths_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithoutStream(startThreeWeeks, endThreeWeeks);
|
||||||
|
assertEquals(17, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenThreeWeekGapMidweekAndWeekendDates_whenUsingStreams_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithStream(startThreeWeeksWeekend, endThreeWeeksWeekend);
|
||||||
|
assertEquals(5, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenThreeWeekGapMidweekAndWeekendDates_whenUsingMaths_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithoutStream(startThreeWeeksWeekend, endThreeWeeksWeekend);
|
||||||
|
assertEquals(5, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenThreeWeekGapWeekendDates_whenUsingStreams_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithStream(startThreeWeeks2, endThreeWeeks2);
|
||||||
|
assertEquals(40, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenThreeWeekGapWeekendDates_whenUsingMaths_thenCalculateWeekdays(){
|
||||||
|
CalculateWeekdays c = new CalculateWeekdays();
|
||||||
|
long result = c.getWorkingDaysWithoutStream(startThreeWeeks2, endThreeWeeks2);
|
||||||
|
assertEquals(40, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -112,6 +112,7 @@
|
||||||
<module>core-java-datetime-string-2</module>
|
<module>core-java-datetime-string-2</module>
|
||||||
<module>core-java-date-operations-2</module>
|
<module>core-java-date-operations-2</module>
|
||||||
<module>core-java-date-operations-3</module>
|
<module>core-java-date-operations-3</module>
|
||||||
|
<module>core-java-date-operations-4</module>
|
||||||
<module>core-java-documentation</module>
|
<module>core-java-documentation</module>
|
||||||
<module>core-java-exceptions</module>
|
<module>core-java-exceptions</module>
|
||||||
<module>core-java-exceptions-2</module>
|
<module>core-java-exceptions-2</module>
|
||||||
|
|
Loading…
Reference in New Issue