我目前正在从事,它的休假管理模块中HR system
有一个get 10 consecutive days (working days except weekends)
休假选项。它是一个J2EE应用程序。
我只想 get 'N' number of consecutive weekdays from the given date
有人知道如何解决这个问题吗?
PS:我没有使用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();
}
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句