说一说我的思路: 先算总数,然后算经历的周数(注意第一天是否为周日以及最后一天是否为周六), 最后根据周数算出周末总天数,然后从总天数里减去周末总天数 OVER
用这个方法试一试 getDiffDaysNoWeekend(Date start, Date end)public class DateUtils { public static int getDiffDays(Date start, Date end) { int rtn = Integer.MIN_VALUE; if (start != null && end != null) { long lngMinMilSec = start.getTime(); long lngMaxMilSec = end.getTime(); rtn = (int) ((lngMaxMilSec - lngMinMilSec) / (1000 * 60 * 60 * 24)); } return rtn; } public static int getDiffDaysWithWeekend(Date start, Date end) { int rtn = getDiffDays(start, end); return rtn != Integer.MIN_VALUE ? rtn + 1 : Integer.MIN_VALUE; } public static int getWeek(Date dt) { int rtn = Integer.MIN_VALUE; Calendar cd = Calendar.getInstance(); cd.setTime(dt); int week = cd.get(Calendar.DAY_OF_WEEK); if (week == 1) { rtn = 7; } else { rtn = week - 1; } return rtn; } public static int getDiffDaysNoWeekend(Date start, Date end) { int rtn = Integer.MIN_VALUE; // Monday 1 -- Sunday 7 int weekStart = getWeek(start); int weekEnd = getWeek(end); int days = getDiffDaysWithWeekend(start, end); if (days > 1) { if (weekStart == weekEnd) { rtn = days - ((days % 7) * 2); } else { int intg = days / 7; int mod = days % 7; switch (weekStart) { case 1: case 2: case 3: case 4: case 5: if (mod > (6 - weekStart)) { mod = 6 - weekStart - mod; if (mod < -2) mod = -2; } else { mod = 0; } rtn = days - intg * 2 + mod; break; case 6: rtn = days - intg * 2 - 2; break; case 7: mod = 7 - (mod - 0); rtn = days - intg * 2 - 1; break; } } } else if(days==1){ if (weekStart == 6 || weekStart == 7) { rtn = 0; } else { rtn = 1; } } return rtn; } }
修正一下 getDiffDaysNoWeekend(Date start, Date end) public static int getDiffDaysNoWeekend(Date start, Date end) { int rtn = Integer.MIN_VALUE; // Total days int days = getDiffDaysWithWeekend(start, end);
// Monday 1 -- Sunday 7 int weekStart = getWeek(start);
// Week count int intg = days / 7;
// left days int mod1 = days % 7;
// left days modified int mod2 = (weekStart+mod1)%7;
int factor = 0; if(mod1!=0 && mod2<weekStart){
// Start date is from Monday 1 -- Saturday 6. if(weekStart!=7){ factor = mod2+1; // Max value is 2 if(factor>2) factor = 2; }else { factor = 1; } } rtn = days - intg*2-factor; return rtn; }
先算总数,然后算经历的周数(注意第一天是否为周日以及最后一天是否为周六),
最后根据周数算出周末总天数,然后从总天数里减去周末总天数 OVER
public static int getDiffDays(Date start, Date end) {
int rtn = Integer.MIN_VALUE;
if (start != null && end != null) {
long lngMinMilSec = start.getTime();
long lngMaxMilSec = end.getTime();
rtn = (int) ((lngMaxMilSec - lngMinMilSec) / (1000 * 60 * 60 * 24));
}
return rtn;
} public static int getDiffDaysWithWeekend(Date start, Date end) {
int rtn = getDiffDays(start, end);
return rtn != Integer.MIN_VALUE ? rtn + 1 : Integer.MIN_VALUE;
} public static int getWeek(Date dt) {
int rtn = Integer.MIN_VALUE;
Calendar cd = Calendar.getInstance();
cd.setTime(dt);
int week = cd.get(Calendar.DAY_OF_WEEK);
if (week == 1) {
rtn = 7;
} else {
rtn = week - 1;
}
return rtn;
} public static int getDiffDaysNoWeekend(Date start, Date end) {
int rtn = Integer.MIN_VALUE; // Monday 1 -- Sunday 7
int weekStart = getWeek(start);
int weekEnd = getWeek(end);
int days = getDiffDaysWithWeekend(start, end); if (days > 1) {
if (weekStart == weekEnd) {
rtn = days - ((days % 7) * 2);
} else {
int intg = days / 7;
int mod = days % 7; switch (weekStart) {
case 1:
case 2:
case 3:
case 4:
case 5:
if (mod > (6 - weekStart)) {
mod = 6 - weekStart - mod;
if (mod < -2)
mod = -2;
} else {
mod = 0;
}
rtn = days - intg * 2 + mod;
break;
case 6:
rtn = days - intg * 2 - 2;
break;
case 7:
mod = 7 - (mod - 0);
rtn = days - intg * 2 - 1;
break;
}
}
} else if(days==1){
if (weekStart == 6 || weekStart == 7) {
rtn = 0;
} else {
rtn = 1;
}
}
return rtn;
}
}
int rtn = Integer.MIN_VALUE; // Total days
int days = getDiffDaysWithWeekend(start, end);
// Monday 1 -- Sunday 7
int weekStart = getWeek(start);
// Week count
int intg = days / 7;
// left days
int mod1 = days % 7;
// left days modified
int mod2 = (weekStart+mod1)%7;
int factor = 0;
if(mod1!=0 && mod2<weekStart){
// Start date is from Monday 1 -- Saturday 6.
if(weekStart!=7){
factor = mod2+1;
// Max value is 2
if(factor>2) factor = 2;
}else {
factor = 1;
}
}
rtn = days - intg*2-factor;
return rtn;
}
计算公式: 工作天数 = 总天数 - 周数*2 - 修正因子。前提:开始日是周几 : 1,2,7,分别代表 周一 到 周日
(1)。 用 总天数(T)整除 7 得到 周数(W): W = T 整除 7
(2)。 用 总天数(T)取余 7 得到 余下天数(MOD1): MOD1 = T 取余 7
注: MOD1 , 0, 1,2,...6
(3)。 用开始日是周几(X) 加上 余下天数(MOD1) 取余 7 得到 修正余数(MOD2): MOD2 = (X+ MOD1) 取余 7
注: MOD2 , 0, 1,2,...6
(4)。 当 MOD1 〉0 and MOD2<开始日是周几(X) 时,修要计算“修正因子(F)“. 否则 不用修正。修正因子(F)最大值为2
周几 1 2 3 4 5 6 7 1 2 3 4 5 6 7
开始日1 1
MOD1 1 2 3 4 5 6 (需要修正 6)
开始日2 2
MOD1 1 2 3 4 5 6 (需要修正 5,6)
开始日3 3
MOD1 1 2 3 4 5 6 (需要修正 4,5,6)
开始日4 4
MOD1 1 2 3 4 5 6 (需要修正 3,4,5,6)
开始日5 5
MOD1 1 2 3 4 5 6 (需要修正 2,3,4,5,6)
开始日6 6
MOD1 1 2 3 4 5 6 (需要修正 1,2,3,4,5,6)
开始日7 7
MOD1 1 2 3 4 5 6 (需要修正 1,2,3,4,5,6)开始日(1-6) 修正因子(F) = MOD2 +1 ,但最大值为 2,
开始日(7) 修正因子(F) = 1
String xingqi = calendar.toString().substring(calendar.toString().indexOf(
"DAY_OF_WEEK=") + 12, calendar.toString().indexOf("DAY_OF_WEEK=") + 13);
int xq = Integer.parseInt(xingqi);
//然后判断
if (xq == 1 || xq == 7){//条件还可以增加月日满足条件的假日如(month==10&&day==1)等等
//处理
}