BAEL-7382 Add code for calculating weekdays between two dates

This commit is contained in:
Sam Gardner 2024-01-15 14:35:18 +00:00
parent eb6e485b58
commit 9231d3611c
4 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,6 @@
## Core Date Operations (Part 4)
This module contains articles about date operations in Java.
### Relevant Articles:

View File

@ -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>

View File

@ -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;
}
}

View File

@ -0,0 +1,83 @@
package com.baeldung.calculateweekdays;
import static junit.framework.TestCase.assertEquals;
import java.time.LocalDate;
import org.junit.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
public void givenTwoDaysOnSameWeekend_whenUsingStreams_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithStream(startTomorrow, endTomorrow);
assertEquals(0, result);
}
@Test
public void givenTwoDaysOnSameWeekend_whenUsingMaths_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithoutStream(startTomorrow, endTomorrow);
assertEquals(0, result);
}
@Test
public void givenAThreeWeekGapMidweekDates_whenUsingStreams_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithStream(startThreeWeeks, endThreeWeeks);
assertEquals(17, result);
}
@Test
public void givenAThreeWeekGapMidweekDates_whenUsingMaths_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithoutStream(startThreeWeeks, endThreeWeeks);
assertEquals(17, result);
}
@Test
public void givenThreeWeekGapMidweekAndWeekendDates_whenUsingStreams_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithStream(startThreeWeeksWeekend, endThreeWeeksWeekend);
assertEquals(5, result);
}
@Test
public void givenThreeWeekGapMidweekAndWeekendDates_whenUsingMaths_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithoutStream(startThreeWeeksWeekend, endThreeWeeksWeekend);
assertEquals(5, result);
}
@Test
public void givenThreeWeekGapWeekendDates_whenUsingStreams_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithStream(startThreeWeeks2, endThreeWeeks2);
assertEquals(40, result);
}
@Test
public void givenThreeWeekGapWeekendDates_whenUsingMaths_calculateWeekdays(){
CalculateWeekdays c = new CalculateWeekdays();
long result = c.getWorkingDaysWithoutStream(startThreeWeeks2, endThreeWeeks2);
assertEquals(40, result);
}
}