计算包括假日的工作日 [英] calculate business days including holidays
问题描述
我需要计算两个日期之间的工作日。
例:我们在7月4日假日(美国)。所以如果我的日期是
date1 = 07/03/2012
date2 = 07/06/2012
不是工作日b / w日期应为1,因为7月4日是假日。
我有一个下面的方法来计算工作日,只计算周末而不是假期。
有任何方法来计算假期也....请帮助我这个。
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;
//如果开始和结束相同,则返回0
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
&& amp; startCal.get(Calendar.DAY_OF_WEEK)!= Calendar.SUNDAY){
++ workDays;
}
} while(startCal.getTimeInMillis()< endCal.getTimeInMillis());
return workDays;
}
包含所有的假日,如上所述。
ArrayList< Integer> holidays = ...
只要在
:
:
startCal.add(Calendar.DAY_OF_MONTH,1);
if(startCal.get(Calendar.DAY_OF_WEEK)!= Calendar.SATURDAY
&& amp; startCal.get(Calendar.DAY_OF_WEEK)!= Calendar.SUNDAY
& .contains((Integer)startCal.get(Calendar.DAY_OF_YEAR))){
++ workDays;
}
} while(startCal.getTimeInMillis()< endCal.getTimeInMillis());
为简单起见,我假设假期
包含格式与 Calendar.DAY_OF_YEAR
相同的日期。
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.
ArrayList<Integer> holidays = ...
Just add a condition to your if
condition in your do-while
:
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
&& !holidays.contains((Integer) startCal.get(Calendar.DAY_OF_YEAR))) {
++workDays;
}
} while (startCal.getTimeInMillis() < endCal.getTimeInMillis());
For simplicity's sake, I've assumed holiday
contains dates in the format identical to Calendar.DAY_OF_YEAR
.
这篇关于计算包括假日的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!