import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.util.LocaleUtil;
+import org.apache.poi.util.Removal;
/**
* A calculator for workdays, considering dates as excel representations.
/**
* Calculate how many workdays are there between a start and an end date, as excel representations, considering a range of holidays.
- *
+ *
* @param start start date.
* @param end end date.
* @param holidays an array of holidays.
/**
* Calculate the workday past x workdays from a starting date, considering a range of holidays.
- *
+ *
* @param start start date.
* @param workdays number of workdays to be past from starting date.
* @param holidays an array of holidays.
}
return endDate.getTime();
}
-
+
/**
* Calculates how many days of week past between a start and an end date.
- *
+ *
* @param start start date.
* @param end end date.
* @param dayOfWeek a day of week as represented by {@link Calendar} constants.
pastDaysOfWeek++;
}
}
- return start < end ? pastDaysOfWeek : -pastDaysOfWeek;
+ return start <= end ? pastDaysOfWeek : -pastDaysOfWeek;
}
/**
* Calculates how many holidays in a list are workdays, considering an interval of dates.
- *
+ *
* @param start start date.
* @param end end date.
* @param holidays an array of holidays.
int nonWeekendHolidays = 0;
double startDay = start < end ? start : end;
double endDay = end > start ? end : start;
- for (int i = 0; i < holidays.length; i++) {
- if (isInARange(startDay, endDay, holidays[i])) {
- if (!isWeekend(holidays[i])) {
+ for (double holiday : holidays) {
+ if (isInARange(startDay, endDay, holiday)) {
+ if (!isWeekend(holiday)) {
nonWeekendHolidays++;
}
}
}
- return start < end ? nonWeekendHolidays : -nonWeekendHolidays;
+ return start <= end ? nonWeekendHolidays : -nonWeekendHolidays;
}
/**
* @return <code>true</code> if date is a holiday, <code>false</code> otherwise.
*/
protected boolean isHoliday(double aDate, double[] holidays) {
- for (int i = 0; i < holidays.length; i++) {
- if (Math.round(holidays[i]) == Math.round(aDate)) {
+ for (double holiday : holidays) {
+ if (Math.round(holiday) == Math.round(aDate)) {
return true;
}
}
* @param aDate a given date.
* @param holidays an array of holidays.
* @return <code>1</code> is not a workday, <code>0</code> otherwise.
+ *
+ * @deprecated POI 3.16 - will be removed, not used in POI itself
*/
+ @Removal(version="3.18")
protected int isNonWorkday(double aDate, double[] holidays) {
return isWeekend(aDate) || isHoliday(aDate, holidays) ? 1 : 0;
}
assertEquals(4, WorkdayCalculator.instance.calculateWorkdays(A_FRIDAY, A_WEDNESDAY, new double[]{ A_SATURDAY, A_SUNDAY }));
}
+ @Test
+ public void testCalculateWorkdaysOnSameDayShouldReturn1ForWeekdays() {
+ final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
+ assertEquals(1, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[0]));
+ }
+
+ @Test
+ public void testCalculateWorkdaysOnSameDayShouldReturn0ForHolidays() {
+ final double A_MONDAY = DateUtil.getExcelDate(d(2017, 1, 2));
+ assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_MONDAY, A_MONDAY, new double[]{ A_MONDAY }));
+ }
+
+ @Test
+ public void testCalculateWorkdaysOnSameDayShouldReturn0ForWeekends() {
+ final double A_SUNDAY = DateUtil.getExcelDate(d(2017, 1, 1));
+ assertEquals(0, WorkdayCalculator.instance.calculateWorkdays(A_SUNDAY, A_SUNDAY, new double[0]));
+ }
+
@Test
public void testCalculateWorkdaysNumberOfDays() {
double start = 41553.0;
final double A_SATURDAY = DateUtil.getExcelDate(d(2011, 12, 10));
assertEquals(1, WorkdayCalculator.instance.pastDaysOfWeek(A_THURSDAY, A_SATURDAY, SATURDAY));
}
-
+
private static Date d(int year, int month, int day) {
Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day, 0, 0, 0);
return cal.getTime();
}
+
+ @Test
+ public void testCalculateNonWeekendHolidays() {
+ final double start = DateUtil.getExcelDate(d(2016, 12, 24));
+ final double end = DateUtil.getExcelDate(d(2016, 12, 31));
+ final double holiday1 = DateUtil.getExcelDate(d(2016, 12, 25));
+ final double holiday2 = DateUtil.getExcelDate(d(2016, 12, 26));
+ int count = WorkdayCalculator.instance.calculateNonWeekendHolidays(start, end, new double[]{holiday1, holiday2});
+ assertEquals("Expected 1 non-weekend-holiday for " + start + " to " + end + " and " + holiday1 + " and " + holiday2,
+ 1, count);
+ }
+
+ @Test
+ public void testCalculateNonWeekendHolidaysOneDay() {
+ final double start = DateUtil.getExcelDate(d(2016, 12, 26));
+ final double end = DateUtil.getExcelDate(d(2016, 12, 26));
+ final double holiday1 = DateUtil.getExcelDate(d(2016, 12, 25));
+ final double holiday2 = DateUtil.getExcelDate(d(2016, 12, 26));
+ int count = WorkdayCalculator.instance.calculateNonWeekendHolidays(start, end, new double[]{holiday1, holiday2});
+ assertEquals("Expected 1 non-weekend-holiday for " + start + " to " + end + " and " + holiday1 + " and " + holiday2,
+ 1, count);
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testIsNonWorkday() throws Exception {
+ final double weekend = DateUtil.getExcelDate(d(2016, 12, 25));
+ final double holiday = DateUtil.getExcelDate(d(2016, 12, 26));
+ final double workday = DateUtil.getExcelDate(d(2016, 12, 27));
+ assertEquals(1, WorkdayCalculator.instance.isNonWorkday(weekend, new double[]{holiday}));
+ assertEquals(1, WorkdayCalculator.instance.isNonWorkday(holiday, new double[]{holiday}));
+ assertEquals(0, WorkdayCalculator.instance.isNonWorkday(workday, new double[]{holiday}));
+ }
}