2016-04-06 68 views
1

我目前正在致力於HR system,並且它在休假管理模塊中有一個選項,用於請假get 10 consecutive days (working days except weekends)。它是一個J2EE應用程序。從給定日期起連續獲得'N'個連續工作日

所有我想要的是get 'N' number of consecutive weekdays from the given date

有誰知道如何實現這個問題?

P.S:我不使用任何第三方庫像JodaTime ..

這裏是我的控制器的單天的假期的應用程序代碼。它與連續的日子無關。但是這張貼在這裏,以證明我在做正經事..

if (action.equals("applyLeave")) { 

    Leave l = new Leave(); 

    int userid = Integer.parseInt(request.getParameter("userid")); 
    int leavetype = Integer.parseInt(request.getParameter("leavetype")); 
    String from = request.getParameter("from"); // from time 
    String to = request.getParameter("to"); // to time 
    double total = Double.parseDouble(request.getParameter("total")); // total hours 

    String r1 = request.getParameter("rep1"); 
    String r2 = request.getParameter("rep2"); 

    if (r1 == null || r1.isEmpty()) { 
     r1 = "0"; 
    } 

    if (r2 == null || r2.isEmpty()) { 
     r2 = "0"; 
    } 

    int rep1 = Integer.parseInt(r1); 
    int rep2 = Integer.parseInt(r2); 

    String reason = request.getParameter("reason"); 
    String date = request.getParameter("date"); 

    l.setUser(userid); 
    l.setLeavetype(leavetype); 
    l.setDate(date); 
    l.setFrom(from); 
    l.setReason(reason); 
    l.setRep1(rep1); 
    l.setRep2(rep2); 
    l.setTo(to); 
    l.setTotal(total); 

    dao.saveLeave(l); 

    // get manager of the department 
    UserDao udao = (UserDao) ctx.getBean("udao"); 
    DepartmentDao ddao = (DepartmentDao) ctx.getBean("depdao"); 
    NotificationDao notificationDao = (NotificationDao) ctx.getBean("notificationDao"); 
    User u = udao.getUserByUserID(userid).get(0); 
    int department = u.getDepartment(); 
    Department d = ddao.getDepartmentByID(department).get(0); 
    int manager = d.getManager(); 

    // save a notification for the respective manager 
    // insert notification 
    String text = u.getFirstname() + " " + u.getLastname() + " has applied for a leave"; 
    Notification n = new Notification(); 
    n.setUserid(manager); 
    n.setFk(dao.getLeaveID()); 
    n.setType(3); 
    n.setText(text); 
    notificationDao.saveNotification(n); 

    PrintWriter out = res.getWriter(); 
    res.setContentType("text/html"); 
    Gson gson = new Gson(); 
    JsonObject myObj = new JsonObject(); 
    myObj.add("message", gson.toJsonTree("Successfully Inserted")); 
    out.println(myObj.toString()); 
    out.close(); 

} 
+0

@Psioniax,我已經完成了大部分的應用程序中的事情,但我竭力讓這個連續數天。我已經搜索谷歌的解決方案,但我找不到任何.. –

+0

編輯您的問題,包括一些代碼 –

+0

@Psioniax,編碼 –

回答

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

+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; 

警告:未編譯或運行此示例,因此可能會導致異常。