Calculating dates given two dates excluding weekend

前端 未结 7 563
予麋鹿
予麋鹿 2020-12-09 00:53

I am using Joda time api in a Spring 3.0 project for to calculate dates. Now I have a start and end date and I want to get everyday exept weekend or Saturday or Sunday betwe

7条回答
  •  忘掉有多难
    2020-12-09 01:25

    This will return the end date by excluding weekends

    public static Date addDaysBySkipWeekend(Date startDate, int numDays) {
            Calendar dateCal = Calendar.getInstance();
            dateCal.setTime(startDate);
            for (int i = 0; i < numDays-1; i++) {
                dateCal.add(dateCal.DATE, 1);
                if(dateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY 
                        || dateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY  ){
                    dateCal.add(dateCal.DATE, 1);
                    i--;
                }
            }
            return dateCal.getTime();
        }
    

提交回复
热议问题