How to calculate next working day in java / How to increment date excluding weekend in java

It may happen that you need to add some days to a particular date and the resultant date is falling on either Saturday or Sunday. So you have to calculate the next working date.

Practical Application : This is usually required in payments industry where the dates are written to files and these files are then uploaded to server but the server rejects the file if the date is a holiday. In such cases the date should be checked to be a holiday before writing it to file and incremented to a working day and then written to the file so that it is not rejected.

Let’s look at the code.

    public String incrementDaysExcludingWeekends(String[] args) throws ParseException {
          // format of date is passed as an argument
          SimpleDateFormat sdf = new SimpleDateFormat(args[1]);
          // base date which will be incremented
          String dateStr = args[0];
          Date date = sdf.parse(dateStr);
          Calendar c = Calendar.getInstance();
          // set calendar time with given date
          c.setTime(date);
          // number of days to increment
          int maxIncrement = Integer.parseInt(args[2]);
          // add days to date 
          c.add(Calendar.DAY_OF_WEEK, maxIncrement);
          // check if the date after addition is a working day. 
          // If not then keep on incrementing it till it is a working day
          while(!isWorkingDay(c.getTime(), c)) {
              c.add(Calendar.DAY_OF_WEEK, 1);
          }
            return sdf.format(c.getTime());
      }

      private boolean isWorkingDay(Date date, Calendar calendar) {
          // set calendar time with given date
          calendar.setTime(date);
          int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
          // check if it is Saturday(day=7) or Sunday(day=1)
          if ((dayOfWeek == 7) || (dayOfWeek == 1)) {
              return false;
          }
          return true;
       }

Let’s tweak in :

  1. The above method incrementDaysExcludingWeekends takes a String array of three arguments : the format of input date, input date itself and the number of dates to increment.
  2. Method isWorkingDay checks a given date to be a working day, that is, it lies between Monday to Friday and takes the date to check and a calendar object.
  3. A java.util.Calendar object can be retrieved by calling its static getInstance() method. Overloaded variants of this method take timezone and locale but if calendar is created using no-arg getInstance() method then it is created using the default timezone and locale of the user’s system.
  4. getTime() method of java.util.Calendar class returns the date object representing the calendar’s object time value.
  5. add() method of java.util.Calendar takes two arguments : the field (or date-time component) to increment (day, month, year, hour, minute, second etc.) and the amount by which to increment it.
  6. Both arguments of java.util.Calendar‘s add() method are Integer (or numeric). It might be a surprise as the first argument is the component to increment such as DAY, MONTH and how it can be numeric. This is because Calendar class has constants representing each field. It has constant for MONTH, DAY, YEAR etc. and these are of type int and each is assigned a value.
    So, in place of Calendar.MONTH to add() method you can pass 2 since MONTH is assigned a value of 2.
  7. format() method, which takes a java.util.Date object as argument, called on java.text.SimpleDateFormat object converts a date into String format. This method belongs to java.text.DateFormat class which is the parent of java.text.SimpleDateFormat.
  8. parse() method which takes a String also belongs to java.text.DateFormat class. It may throw a java.text.ParseException so you need to catch it in the method where parse() is used or throw it from the method.

Hope you liked the post and some of you may relate to it or have faced this task. Provide your comment for any queries / suggestions.

Leave a Reply