Here you can find the source of getWorkingDays(Calendar start, Calendar end, final Long[] holidays)
Parameter | Description |
---|---|
start | a parameter |
end | a parameter |
holidays | (Optional) Array of holidays |
public static Long getWorkingDays(Calendar start, Calendar end, final Long[] holidays)
//package com.java2s; //License from project: LGPL import java.util.*; public class Main { /**/*from w w w. j a va2 s . com*/ * Milliseconds=14 * */ public static final int MILLISECOND = Calendar.MILLISECOND; /** * Seconds=13 * */ public static final int SECOND = Calendar.SECOND; /** * Minute=12 * */ public static final int MINUTE = Calendar.MINUTE; /** * Hour=11 * */ public static final int HOUR = Calendar.HOUR_OF_DAY; /** * Day=5 * */ public static final int DAY = Calendar.DAY_OF_MONTH; /** * MONTH=2 * */ public static final int MONTH = Calendar.MONTH; /** * Year=1 * */ public static final int YEAR = Calendar.YEAR; /** * no-working days */ private static final int[] highDays = { Calendar.SUNDAY, Calendar.SATURDAY }; /** * @param startTime * @param endTime * @param holidays (Optional) Array of holidays * @return */ public static Long getWorkingDays(final long startTime, final long endTime, final Long[] holidays) { Calendar startCalendar = Calendar.getInstance(); startCalendar.setTimeInMillis(startTime); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTimeInMillis(endTime); return getWorkingDays(startCalendar, endCalendar, holidays); } /** * @param startDate * @param finalDate * @param holidays (Optional) Array of holidays * @return */ public static Long getWorkingDays(final Date startDate, final Date finalDate, final Long[] holidays) { return getWorkingDays(startDate.getTime(), finalDate.getTime(), holidays); } /** * @param start * @param end * @param holidays (Optional) Array of holidays * @return */ public static Long getWorkingDays(Calendar start, Calendar end, final Long[] holidays) { Long result = 0L; // day of weeks int startDayOfWeek = start.get(Calendar.DAY_OF_WEEK); int endDayOfWeek = end.get(Calendar.DAY_OF_WEEK); // adjust calendars to fist working day if (!isWorkingDay(start.getTime(), holidays)) { start = nextWorkingDay(start, holidays); } if (!isWorkingDay(end.getTime(), holidays)) { end = previousWorkingDay(end, holidays); } // difference in day beetween 2 date Long dayDiff = Math.abs(dateDiff(start.getTime(), end.getTime()) / (1000L * 60L * 60L * 24L)); // number of week-ends in period Long weekEnds = dayDiff / 7; if (startDayOfWeek < endDayOfWeek) { weekEnds++; } result = dayDiff - (weekEnds * highDays.length); return result; } /** * Return true if the day is a working day * * @param date Date * @param holidays Array of holidays * @return Return true if the day is a working day */ public static boolean isWorkingDay(final Date date, final Long[] holidays) { return isWorkingDay(date.getTime(), holidays); } /** * Return true if the day is a working day * * @param time Long - time in milliseconds * @param holidays Array of holidays * @return Return true if the day is a working day */ public static boolean isWorkingDay(final long time, final Long[] holidays) { final Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); return isWorkingDay(calendar, holidays); } public static boolean isWorkingDay(final Calendar calendar, final Long[] holidays) { // check if in holiday if (null != holidays) { final Calendar holiday = Calendar.getInstance(); for (final long d : holidays) { holiday.setTimeInMillis(d); if (holiday.get(Calendar.YEAR) == calendar.get(Calendar.YEAR) && holiday.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) && holiday.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) { return false; } } } // check day of week final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); for (final int i : highDays) { if (i == dayOfWeek) { return false; } } return true; } /** * @param date * @return */ public static Calendar nextWorkingDay(final Date date, final Long[] holidays) { return nextWorkingDay(date.getTime(), holidays); } /** * @param time * @return */ public static Calendar nextWorkingDay(final Long time, final Long[] holidays) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); return nextWorkingDay(calendar, holidays); } /** * @param calendar * @param holidays (Optional) Array of holidays * @return */ public static Calendar nextWorkingDay(final Calendar calendar, final Long[] holidays) { // move forward calendar of one day calendar.add(DAY, 1); while (!isWorkingDay(calendar, holidays)) { calendar.add(DAY, 1); } return calendar; } public static Calendar previousWorkingDay(final Date date, final Long[] holidays) { return previousWorkingDay(date.getTime(), holidays); } public static Calendar previousWorkingDay(final Long time, final Long[] holidays) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time); // move forward calendar of one day calendar.add(DAY, -1); while (!isWorkingDay(calendar.getTimeInMillis(), holidays)) { calendar.add(DAY, -1); } return calendar; } /** * @param calendar * @param holidays (Optional) Array of holidays * @return */ public static Calendar previousWorkingDay(final Calendar calendar, final Long[] holidays) { // move backward calendar of one day calendar.add(DAY, -1); while (!isWorkingDay(calendar.getTimeInMillis(), holidays)) { calendar.add(DAY, -1); } return calendar; } /** * Calculate difference in milliseconds from 2 dates. * * @param date1 First date * @param date2 Second date * @return Difference between first date and second date in milliseconds */ public static long dateDiff(final Date date1, final Date date2) { long result = date1.getTime() - date2.getTime(); return result; } /** * Calculate difference from 2 dates.<br> * Result is in "measureUnit" measure unit. (Ex: DAY) * * @param date1 First date * @param date2 Second date * @param measureUnit Measure unit. Ex: DateUtility.DAY * @return Difference between first date and second date. */ public static double dateDiff(final Date date1, final Date date2, int measureUnit) { long diff = dateDiff(date1, date2); double result = 0d; switch (measureUnit) { case MILLISECOND: result = (double) diff; break; case SECOND: result = (double) (diff / 1000d); break; case MINUTE: result = (double) (diff / (1000d * 60L)); break; case HOUR: result = (double) (diff / (1000d * 60d * 60d)); break; case DAY: result = (double) (diff / (1000d * 60d * 60d * 24d)); break; } return result; } }