计算从beginDate到endDate的工作日

public static int[] work_rest_all(String beginDate, String endDate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 代表自然日(一天就代表一个自然日)
int natureDay = 0;
// 代表休息日,假定周六周日休息
int restDay = 0;
// 如果开始时间为空,则为当前日期
Calendar now = Calendar.getInstance();
if (beginDate != null) {
try {
now.setTime(sdf.parse(beginDate));
} catch (ParseException e) {
e.printStackTrace();
throw new ParseException("\n"
+ "DateUtils.work_rest_all() was error", 1);
}
}
Calendar end = Calendar.getInstance();
try {
Date date = sdf.parse(endDate);
end.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
throw new ParseException("\n"
+ "DateUtils.work_rest_all() was error", 2);
}
String ss = sdf.format(end.getTime());
if (now.before(end)) {
while (!sdf.format(now.getTime()).equals(ss)) {
if (now.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
restDay++;
}
natureDay++;
now.add(Calendar.DAY_OF_YEAR, 1);
}
}
int[] result = { natureDay - restDay, restDay, natureDay };
return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值