Skip to content

Commit c3ad626

Browse files
committed
- Separated add and subtract days into two methods for simplification
1 parent 82fd2d9 commit c3ad626

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

core-java-modules/core-java-date-operations/src/main/java/com/baeldung/datetime/AddSubtractDaysSkippingWeekendsUtils.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@
55

66
public class AddSubtractDaysSkippingWeekendsUtils {
77

8-
public static LocalDate addSubtractDaysSkippingWeekendsIterativeMethod(LocalDate date, int days) {
8+
public static LocalDate addDaysSkippingWeekends(LocalDate date, int days) {
99
LocalDate result = date;
1010
int addedDays = 0;
11-
int absDays = Math.abs(days);
12-
int day = (days > 0) ? 1 : -1;
13-
while (addedDays < absDays) {
14-
result = result.plusDays(day);
11+
while (addedDays < days) {
12+
result = result.plusDays(1);
1513
if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY || result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
1614
++addedDays;
1715
}
1816
}
1917
return result;
2018
}
19+
20+
public static LocalDate subtractDaysSkippingWeekends(LocalDate date, int days) {
21+
LocalDate result = date;
22+
int subtractedDays = 0;
23+
while (subtractedDays < days) {
24+
result = result.minusDays(1);
25+
if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY || result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
26+
++subtractedDays;
27+
}
28+
}
29+
return result;
30+
}
2131
}

core-java-modules/core-java-date-operations/src/test/java/com/baeldung/datetime/AddSubtractDaysSkippingWeekendsUtilsUnitTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
public class AddSubtractDaysSkippingWeekendsUtilsUnitTest {
1010

1111
@Test
12-
public void givenLocalDateAndDaysToAdd_thenAddDaysSkippingWeekendsUsingIterativeMethod() {
12+
public void givenLocalDateAndDaysToAdd_thenAddDaysSkippingWeekends() {
1313
LocalDate initialDate = LocalDate.of(2019, 11, 7);
1414
LocalDate expectedDate = LocalDate.of(2019, 11, 13);
15-
LocalDate result = AddSubtractDaysSkippingWeekendsUtils.addSubtractDaysSkippingWeekendsIterativeMethod(initialDate, 4);
15+
LocalDate result = AddSubtractDaysSkippingWeekendsUtils.addDaysSkippingWeekends(initialDate, 4);
1616
assertEquals(expectedDate, result);
1717
}
1818

1919
@Test
20-
public void givenLocalDateAndDaysToAdd_thenSubtractDaysSkippingWeekendsUsingIterativeMethod() {
20+
public void givenLocalDateAndDaysToSubtract_thenSubtractDaysSkippingWeekends() {
2121
LocalDate initialDate = LocalDate.of(2019, 11, 7);
2222
LocalDate expectedDate = LocalDate.of(2019, 11, 1);
23-
LocalDate result = AddSubtractDaysSkippingWeekendsUtils.addSubtractDaysSkippingWeekendsIterativeMethod(initialDate, -4);
23+
LocalDate result = AddSubtractDaysSkippingWeekendsUtils.subtractDaysSkippingWeekends(initialDate, 4);
2424
assertEquals(expectedDate, result);
2525
}
2626

0 commit comments

Comments
 (0)