Skip to content

Instantly share code, notes, and snippets.

@vikytech
Created February 13, 2021 05:56
Show Gist options
  • Save vikytech/e115802ad7d187ab085223719bbef1ac to your computer and use it in GitHub Desktop.
Save vikytech/e115802ad7d187ab085223719bbef1ac to your computer and use it in GitHub Desktop.
Date Util to add working days for given date
import lombok.NoArgsConstructor;
import java.time.DayOfWeek;
import java.time.LocalDate;
import static java.time.DayOfWeek.SATURDAY;
import static java.time.DayOfWeek.SUNDAY;
import static lombok.AccessLevel.PRIVATE;
@NoArgsConstructor(access = PRIVATE)
public class DateUtil {
public static final int ONE_DAY = 1;
public static LocalDate getNextWorkingDay(LocalDate date) {
if (isWeekEnd(date)) {
return getNextWorkingDay(date.plusDays(ONE_DAY));
}
return date;
}
public static LocalDate addWorkingDays(LocalDate startDate, int numberOfWorkingDays) {
return getWorkingDays(startDate, numberOfWorkingDays).minusDays(ONE_DAY);
}
private static LocalDate getWorkingDays(LocalDate startDate, int numberOfWorkingDays) {
if (numberOfWorkingDays < ONE_DAY) {
return startDate.plusDays(ONE_DAY);
}
LocalDate tempDate = startDate;
int daysAdded = 0;
while (!isWeekEnd(tempDate)) {
tempDate = tempDate.plusDays(ONE_DAY);
daysAdded++;
if (daysAdded == numberOfWorkingDays) {
return tempDate;
}
}
if (daysAdded <= numberOfWorkingDays) {
tempDate = getWorkingDays(tempDate.plusDays(ONE_DAY), numberOfWorkingDays - daysAdded);
}
return tempDate;
}
private static boolean isWeekEnd(LocalDate date) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
return dayOfWeek == SATURDAY || dayOfWeek == SUNDAY;
}
}
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.time.LocalDate;
import java.util.stream.Stream;
import static com.generali.dol.util.DateUtil.addWorkingDays;
import static com.generali.dol.util.DateUtil.getNextWorkingDay;
import static java.time.Month.FEBRUARY;
import static java.time.Month.MARCH;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DateUtilTest {
public static final int NUMBER_OF_WORKING_DAYS = 10;
@Test
void shouldAddTenWorkingDaysFromMidWeek() {
final LocalDate actualStartDate = LocalDate.of(2021, FEBRUARY, 11);
final LocalDate expectedEndDate = LocalDate.of(2021, FEBRUARY, 24);
assertEquals(expectedEndDate, addWorkingDays(actualStartDate, NUMBER_OF_WORKING_DAYS));
}
@Test
void shouldAddTenWorkingDaysIfTodayStartFromWeekend() {
final LocalDate actualStartDate = LocalDate.of(2021, FEBRUARY, 27);
final LocalDate expectedEndDate = LocalDate.of(2021, MARCH, 12);
assertEquals(expectedEndDate, addWorkingDays(actualStartDate, NUMBER_OF_WORKING_DAYS));
}
@Test
void shouldAddTenWorkingDaysIfTodayIsFriday() {
final LocalDate actualStartDate = LocalDate.of(2021, FEBRUARY, 12);
final LocalDate expectedEndDate = LocalDate.of(2021, FEBRUARY, 25);
assertEquals(expectedEndDate, addWorkingDays(actualStartDate, NUMBER_OF_WORKING_DAYS));
}
@Test
void shouldReturnSameDayIfTodayIsFridayAndWorkingDayRequiredIsOne() {
final LocalDate actualStartDate = LocalDate.of(2021, FEBRUARY, 12);
final LocalDate expectedEndDate = LocalDate.of(2021, FEBRUARY, 12);
assertEquals(expectedEndDate, addWorkingDays(actualStartDate, 1));
}
@Test
void shouldReturnSameDayIfTodayIsFridayAndWorkingDayRequiredIsZero() {
final LocalDate actualStartDate = LocalDate.of(2021, FEBRUARY, 12);
final LocalDate expectedEndDate = LocalDate.of(2021, FEBRUARY, 12);
assertEquals(expectedEndDate, addWorkingDays(actualStartDate, 0));
}
@ParameterizedTest
@MethodSource("generateDatesForWorkingDay")
void shouldReturnNextOrSameWorkingDayDependingOnWeekend(int startDayDate, int endDayDate) {
final LocalDate actualStartDate = LocalDate.of(2021, FEBRUARY, startDayDate);
final LocalDate expectedEndDate = LocalDate.of(2021, FEBRUARY, endDayDate);
assertEquals(expectedEndDate, getNextWorkingDay(actualStartDate));
}
private static Stream<Arguments> generateDatesForWorkingDay() {
return Stream.of(
Arguments.of(12, 12),
Arguments.of(13, 15),
Arguments.of(14, 15)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment