得到N'从给定日期开始的连续工作日数

时间:2016-04-06 07:17:33

标签: java date

我目前正在使用CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, _mainView.frame.origin) ) { [self.scrollView scrollRectToVisible:_mainView.frame animated:YES]; } ,并且在其休假管理模块中可以选择HR system。它是一个J2EE应用程序。

我想要的只是get 10 consecutive days (working days except weekends)

有人知道如何解决这个问题吗?

P.S:我没有使用像JodaTime这样的第三方库..

这是我的控制器单日休假申请代码。它与连续几天的事情无关。但是在这里发布这个以证明我正在做一些严肃的事情......

get 'N' number of consecutive weekdays from the given date

2 个答案:

答案 0 :(得分:1)

不确定您是否需要标准java中的解决方案(即使用javascript标记),但我想您可以按照以下方式执行:

int numberOfDays = 10;

// Get current date
Calendar calendar = Calendar.getInstance();

//Repeat until all consecutive weekdays consumed
while(numberOfDays >0) {
    int day = calendar.get(Calendar.DAY_OF_WEEK);

    if((day != Calendar.SUNDAY) && (DAY != Calendar.SATURDAY)) {
        numberOfDays--;
    }

    calendar.add(Calendar.DAY,1);
}

// Calendar now contains the date after consuming all 
// consecutive week days
return calendar;

警告:尚未编译或运行此示例,因此可能会导致异常。

答案 1 :(得分:1)

List<Date> holidays = conf.getHolidays();
List<Date> nWorkingDays = new ArrayList<>();

// get the current date without the hours, minutes, seconds and millis
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

// iterate over the dates from now and check if each day is a business day
int businessDayCounter = 0
while (businessDayCounter < n) { //You want n working days
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY && !holidays.contains(cal.getTime())) {
        businessDayCounter++;
        nWorkingDays.add(cal.getTime());
    }
    cal.add(Calendar.DAY_OF_YEAR, 1);
}

return nWorkingDays;

改编自这个答案:https://stackoverflow.com/a/15626124/1364747

相关问题