getWorkDays(Date startDt, Date endDt)


	public static int getWorkDays(Date startDt, Date endDt) {
		Calendar startCal, endCal;
		startCal = Calendar.getInstance();
		startCal.setTime(startDt);
		endCal = Calendar.getInstance();
		endCal.setTime(endDt);
		int workDays = 0;

		// Return 0 if start and end are the same
		if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
			return 0;
		}
		// Just in case the dates were transposed this prevents infinite loop
		if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
			startCal.setTime(endDt);
			endCal.setTime(startDt);
		}

		do {
			startCal.add(Calendar.DAY_OF_MONTH, 1);
			if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
					&& startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
				++workDays;
			}
		} while (startCal.getTimeInMillis() < endCal.getTimeInMillis());

		return workDays;
	}

SAMPLE:

		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy");
		Date date = dateFormat.parse("4.5.2012");
		Date date2 = dateFormat.parse("8.5.2012");
		System.out.println(BusinessDayUtils.getWorkDays(date, date2));
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...