i need to calculate the business days between two dates. ex : we have holiday(in USA) on july4th. so if my dates are date1 = 07/03/2012 date2 = 07/06/2012
no of business days b/w these dates should be 1 since july4th is holiday.
i have a below method to calclulate the business days which will only counts week ends but not holidays. is there any way to calculate holidays also....please help me on this.
public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {
Calendar startCal;
Calendar endCal;
startCal = Calendar.getInstance();
startCal.setTime(startDate);
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 {
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;
}
Let's pretend you have a list containing all the holidays, as you mentioned.
Just add a condition to your
if
condition in yourdo-while
:For simplicity's sake, I've assumed
holiday
contains dates in the format identical toCalendar.DAY_OF_YEAR
.The fastest way I've found is using some math (borrowing from the SO post):
And:
I put something more complete together here.
I don't have any code samples or anything like that, but I did some searching for you and came across this Stack Overflow thread that has some links to web services that can return holiday dates for you, which may help you get to where you need to be: National holiday web service
The top answer in that thread links to this web service: http://www.holidaywebservice.com/
I'm not sure if using a web service for this type of thing is overkill or not, but surely there is a better way. I apologize, I am not the most experienced programmer so I can't help you as much as I'd like to.
Nager.Date
You can use the JSON API of Nager.Date project. It supports USA, Canada and Europe. The data available for every year you can save the information in your own database.
Example
PublicHoliday.class
Example JSON data retrieved.
Since the accepted answer still uses the obsolete
Calendar
class – here's my two cents using the new Java Date and Time API.You have to get the dates of the holidays from somewhere, there is no standard Java library for it. That would be too localized anyway, since holidays heavily depends on your country and region. (except for widely known holidays, such as Christmas or Easter).
You could get them from a holiday API, for instance. In the code below I've hardcoded them as a
List
ofLocalDate
s.Java 9
Java 8
The method
LocalDate.datesUntil
is not available in Java 8, so you have to obtain a stream of all dates between those two dates in a different manner. We must first count the total number of days in between using theChronoUnit.DAYS.between
method.Then we must generate a sequence of integers exactly as long as the number of days between the start and end date, and then create
LocalDate
s from it, starting at the start date.Now we have a
Stream<LocalDate>
, and you can then use the remaining part of the Java 9 code.