0

How can I increment the day with skipping weekends. I mean if day=Friday then day+1=Monday. Please take a look at my increment method which I increment a calendar day and not a Business day

public Date  incDay( Date date){
Calendar cal = Calendar.getInstance(); 
cal.setTime(date); 
cal.add(Calendar.DATE, 1);
return cal.getTime();
}

I need to modify this method for resolve this issue.

Update:

I update my method like this

public Date incDay(Date date){
    final Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    // public final static int FRIDAY = 6;
    final int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayOfWeek == Calendar.FRIDAY) {
        cal.add(Calendar.DATE, 3);
    }else{
        cal.add(Calendar.DATE, 1);
    }
    System.out.println(cal.getTime());
    return cal.getTime();
}

Main():

public static void main(String[] args) throws ParseException {

Date d=incBusiness(new Date(2017, 02, 17));//2017/02/18

}

I got 2017/02/18 instead of 2017/02/20

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abderrahim
  • 651
  • 2
  • 11
  • 25
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) and [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 14 '17 at 22:21

2 Answers2

0

Calendar class has constants to check the day of the week:

FRIDAY is the 6th day of the week, doing an if-else can resolve the issue...

    public static void foo() throws ParseException {
    String dateString = "2017/02/17";
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");// "2017/02/17";
    Date date = df.parse(dateString);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
        cal.add(Calendar.DATE, 3);
    } else {
        cal.add(Calendar.DATE, 1);
    }
    System.out.println(cal.getTime());
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Gets date instance and add no. of days excluding weekends. Set date to next monday if provided date is weekend.

public Date addDays(Date date, int days){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    //set date to next monday if provided date day is weekend
    //use this section according to your need.
    if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
        cal.add(Calendar.DATE,2);
        //days-= 2;
    }else if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
        cal.add(Calendar.DATE,1);
        //days--;
    }

    //add days one by one
    while(days > 0){
        //if current day is friday add 3 days to skip saturday and sunday
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
            cal.add(Calendar.DATE,3);
        //else add one day
        }else{
            cal.add(Calendar.DATE,1);
        }
        //decrements day counter 
        days--;
    }

    return cal.getTime();
}
Asad Sarwar
  • 533
  • 3
  • 10