Here you can find the source of getWorkingDaysBetweenTwoDates(Date startDate, Date endDate)
Original code from source: http://stackoverflow.com/questions/4600034/calculate -number-of-weekdays-between-two-dates-in-java
Parameter | Description |
---|---|
startDate | a parameter |
endDate | a parameter |
public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate)
//package com.java2s; /*/* w w w . ja va 2 s.co m*/ Copyright (c) 2012,2013 Mirco Attocchi This file is part of WebAppCommon. WebAppCommon is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. WebAppCommon is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with WebAppCommon. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Calendar; import java.util.Date; public class Main { /** * Original code from source: * http://stackoverflow.com/questions/4600034/calculate * -number-of-weekdays-between-two-dates-in-java * * @param startDate * @param endDate * @return */ public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) { startDate = setTime(startDate, 12, 0); endDate = setTime(endDate, 12, 0); Calendar startCal = Calendar.getInstance(); startCal.setTime(startDate); Calendar endCal = Calendar.getInstance(); endCal.setTime(endDate); int workDays = 0; // Return 0 if start and end are the same if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) { return 0; } if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) { startCal.setTime(endDate); endCal.setTime(startDate); } do { // excluding start date 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()); // excluding // end // date return workDays; } /** * Set time of a Date * * @param aDate * @param hours * @param minutes * @return */ public static Date setTime(Date aDate, int hours, int minutes) { Calendar c = Calendar.getInstance(); c.setTime(aDate); c.set(Calendar.HOUR_OF_DAY, hours); c.set(Calendar.MINUTE, minutes); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } }