I highly recommend using Joda-Time for anything related to date processing in Java, because it has many useful features to make the code less complicated.
This code uses JodaTime:
public static final List<Integer> NON_WORKING_DAYS; static { List<Integer> nonWorkingDays = new ArrayList<Integer>(); nonWorkingDays.add(DateTimeConstants.SATURDAY); nonWorkingDays.add(DateTimeConstants.SUNDAY); NON_WORKING_DAYS = Collections.unmodifiableList(nonWorkingDays); } public static Minutes getMinsBetween(DateTime d1, DateTime d2, boolean onlyBusinessDays) { BaseDateTime startDate = onlyBusinessDays && !isBusinessDay(d1) ? new DateMidnight(d1) : d1; BaseDateTime endDate = onlyBusinessDays && !isBusinessDay(d2) ? new DateMidnight(d2) : d2; Minutes minutes = Minutes.minutesBetween(startDate, endDate); if (onlyBusinessDays) { DateTime d = new DateTime(startDate); while (d.isBefore(endDate)) { if (!isBusinessDay(d)) { Duration dayDuration = new Duration(d, d.plusDays(1)); minutes = minutes.minus(int) dayDuration.getStandardMinutes()); } d = d.plusDays(1); } } return minutes; } private static boolean isBusinessDay(DateTime dateToCheck) { return !NON_WORKING_DAYS.contains(dateToCheck.dayOfWeek().get()); }
When this code is verified, it gives the following results:
DateTime d1 = new DateTime(2013, 1, 4, 18, 0); // a Friday, 6 pm DateTime d2 = new DateTime(2013, 1, 7, 6, 0); // the following Monday, 6 am Minutes minutes = getMinsBetween(d1, d2, true); System.out.println(minutes.toStandardHours().getHours()); // outputs "12" (in hours) d1 = new DateTime(2013, 1, 5, 12, 0); // a Saturday, 12 pm d2 = new DateTime(2013, 1, 6, 12, 0); // the following Sunday, 12 pm minutes = getMinsBetween(d1, d2, true); System.out.println(minutes.toStandardHours().getHours()); // outputs "0" (in hours) d1 = new DateTime(2013, 1, 5, 12, 0); // a Saturday, 12 pm d2 = new DateTime(2013, 1, 7, 6, 0); // the following Monday, 6 am minutes = getMinsBetween(d1, d2, true); System.out.println(minutes.toStandardHours().getHours()); // outputs "6" (in hours)
I just checked the case where the month changes on weekends: from Friday, March 29 (6 hours) to Monday, April 1 (6 hours):
d1 = new DateTime(2013, 3, 29, 18, 0); d2 = new DateTime(2013, 4, 1, 6, 0); minutes = getMinsBetween(d1, d2, true); System.out.println(minutes.toStandardHours().getHours());
The result is 12 hours, so it works to change the month.
My first solution did not handle daylight saving time correctly. We must determine the duration of each actual day when subtracting minutes, because days with a change in daylight saving time will not be equal to 24h:
if (!isBusinessDay(d)) { Duration dayDuration = new Duration(d, d.plusDays(1)); minutes = minutes.minus(int) dayDuration.getStandardMinutes()); }