How can I add business days to the current date in Java?

后端 未结 14 1352
Happy的楠姐
Happy的楠姐 2020-12-03 02:05

How can I add business days to the current date in Java?

public Calendar addBusinessDate(Calendar cal, int days) {
//
// code goes over here
//
}
         


        
14条回答
  •  离开以前
    2020-12-03 02:42

    //supports negative numbers too.

    private Calendar addBusinessDay(final Calendar cal, final Integer numBusinessDays)
    {
        if (cal == null || numBusinessDays == null || numBusinessDays.intValue() == 0)
        {
            return cal;
        }
        final int numDays = Math.abs(numBusinessDays.intValue());
        final int dateAddition = numBusinessDays.intValue() < 0 ? -1 : 1;//if numBusinessDays is negative
        int businessDayCount = 0;
        while (businessDayCount < numDays)
        {
            cal.add(Calendar.DATE, dateAddition);
    
            //check weekend
            if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            {
                continue;//adds another day
            }
    
            //check holiday
            if (isHoliday(cal))//implement isHoliday yourself
            {
                continue;//adds another day
            }
    
            businessDayCount++;
        }
        return cal;
    }
    

提交回复
热议问题