`
yanyanquan
  • 浏览: 441974 次
  • 性别: Icon_minigender_1
  • 来自: 江门
社区版块
存档分类
最新评论

计算从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;
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics