java 时间间隔 工作日,计算Java中两个日期之间的工作日数

Can anyone point me to some Java snippet wherein i can get business (except Sat and Sun) days between two dates.

解决方案

public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {

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;

}

Start date and end date are exclusive, Only the days between given

dates will be counted. Start date and end date will not be included.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值