How to calculate number of working days for any month in java? My input would be month in int. For example - to get the number of working days in September, my input would be 9. I done have the start or end dates. Weekends are Saturday and Sunday. Please help.
Asked
Active
Viewed 744 times
-10
-
3Please provide your code attempt – karen Sep 21 '18 at 08:27
-
3To get the accurate data,you also need to input the `year` – flyingfox Sep 21 '18 at 08:27
-
3Do you have to account for national holidays? – hrs Sep 21 '18 at 08:27
-
Your only input is the month as an `int`, which must mean you are only interested in the current year, right? – deHaar Sep 21 '18 at 08:27
-
2look at hear https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java – Ashvin solanki Sep 21 '18 at 08:30
-
@lucumt - Assuming year is current year – user3649712 Sep 21 '18 at 10:36
-
@hrs - Holidays not to be taken in account. only Saturdays and Sundays to be excluded – user3649712 Sep 21 '18 at 10:37
-
@deHaar - Yes Correct. – user3649712 Sep 21 '18 at 10:38
-
@Ashvinsolanki - Dont want to input start date and end date, only month – user3649712 Sep 21 '18 at 10:38
-
@user3649712 u can modify as u want – Ashvin solanki Sep 21 '18 at 10:53
1 Answers
0
This approach could be not the most optimal, but at least avoids any loops. I tried to solve it literally, so here's the general idea, hope it fits you:
static int countHolidays(int month){
//assuming month is 1-12
Calendar firstDayCal = Calendar.getInstance();
Calendar lastDayCal = Calendar.getInstance();
firstDayCal.set(Calendar.MONTH, month-1);//here we should put 0-11;
lastDayCal.set(Calendar.MONTH, month-1);
int firstDay = firstDayCal.getActualMinimum(Calendar.DAY_OF_MONTH);
int lastDay = firstDayCal.getActualMaximum(Calendar.DAY_OF_MONTH);
firstDayCal.set(Calendar.DAY_OF_MONTH, firstDay);
lastDayCal.set(Calendar.DAY_OF_MONTH, lastDay);
//any month have no less than 28 days, so 4 full weeks - so 8 weekends.
int total = 8;
switch (lastDay) {
case 29:
//leap-year february can have one extra holiday if it starts on sunday or ends on saturday
if ((firstDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || (lastDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)){
total++;
} break;
case 30:
//30-day month can have one extra holiday if it starts on sunday or ends on saturday...
if ((firstDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || (lastDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)){
total++;
//...or two extra holiday if it starts on saturday or ends on sunday
} else if ((firstDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (lastDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)){
total = total + 2;
} break;
case 31:
//31-day month can have one extra holiday if it starts on sunday or ends on saturday...
if ((firstDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || (lastDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)){
total++;
//...or two extra holiday if it starts on (friday or saturday) or ends on (sunday or monday)
} else if (((firstDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) || (firstDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)) ||
((lastDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || (lastDayCal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY))) {
total = total + 2;
} break;
default:
break;
}
return lastDay-total;//as we need workingDays, not weekends
}

Сергей Крылов
- 1
- 3