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

后端 未结 14 1353
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:47

    This one works for me, short and simple:

    public static Date getBusinessDay(final Date date, final int businessDaysFromDate) {
    
    final int max = 60;
    if (date == null) {
      return getBusinessDay(new Date(), businessDaysFromDate);
    } else if (date != null && (businessDaysFromDate < 0 || businessDaysFromDate > max)) {
      return getBusinessDay(date, 0);
    } else {
      final Calendar baseDateCal = Calendar.getInstance();
      baseDateCal.setTime(date);
      for (int i = 1; i <= businessDaysFromDate; i++) {
        baseDateCal.add(Calendar.DATE, 1);
        while (baseDateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || baseDateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
          baseDateCal.add(Calendar.DATE, 1);
        }
      }
      return baseDateCal.getTime();
    }
    

    }

提交回复
热议问题