How to add Business hours to Date considering not adding weekends ? - Java

前端 未结 7 952
盖世英雄少女心
盖世英雄少女心 2020-12-12 04:47

I want to add certain number of hours to date, ignoring the weekends

For example,

(Friday 18:00) + 48 = (Tuseday 18:00) (Saturday and Sunday are ignored)

7条回答
  •  遥遥无期
    2020-12-12 05:22

    you need to handle it in custom way:

      public Date getTaskEndTime(Calendar startDate, int hours) {
            Calendar endTime = Calendar.getInstance();
            endTime.setTime(startDate.getTime());
            endTime.add(Calendar.HOUR, hours); // add 2 for saturday and sunday
            int dayOfWeek = endTime.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek == Calendar.SATURDAY) {
                endTime.add(Calendar.DATE, 2); // add 2 for saturday and sunday
            } else if (dayOfWeek == Calendar.SATURDAY) {
                endTime.add(Calendar.DATE, 1); // add 1 for sunday
            }
            return endTime.getTime();
        }
    

提交回复
热议问题