How to add N working days to the current date in Java?



Problem Description :


Write a program in Java that adds N working days to the current date and prints new date.

Java program that adds n working days in current date.
Add n working days in current date

Concept :


There are 5 working days per week (Monday, Tuesday, Wednesday, Thursday, Friday). Here in a loop we are adding 1 to the current date and checking day of that date. If it is Sunday or Saturday then we are doing increment to the value of N because we are interested in working days.
  • We need to do addition to current date using add() method of Calendar class that accepts DAY_OF_MONTH constant and number of days that you want to add.
  • For getting day, we need to use DAY_OF_WEEK constant of Calendar class.


Recommended :




Java program :


package com.javamultiplex.datetime;

import java.util.Calendar;
import java.util.Scanner;

/**
 * 
 * @author Rohit Agarwal
 * @category Date and Time
 * @Problem How to add N working days to the current date?
 * 
 */
public class AddNWorkingDays {

 public static void main(String[] args) {

  Scanner input = null;
  try {
   input = new Scanner(System.in);
   System.out.println("Enter number of working days : ");
   int limit = input.nextInt();
   int orignalLimit = limit;
   // Create Calendar class instance
   Calendar calendar = Calendar.getInstance();
   System.out.println("Current Date : " + calendar.getTime());
   for (int i = 1; i <= limit; i++) {
    // Adding 1 day to calendar.
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    /*
     * calendar.get(Calendar.DAY_OF_WEEK) = 1 (Sunday)
     * calendar.get(Calendar.DAY_OF_WEEK) = 7 (Saturday)
     * 
     * If day is either Sunday or Saturday then it is non working
     * day so Increasing the limit to compensate working days.
     */
    if (calendar.get(Calendar.DAY_OF_WEEK) == 1
      || calendar.get(Calendar.DAY_OF_WEEK) == 7) {
     limit++;
    }
   }
   System.out.println("After " + orignalLimit
     + " working day/s Date : " + calendar.getTime());
  } finally {
   if (input != null) {
    input.close();
   }
  }

 }

}


Output :


Output of Java Program that adds N working days to the current date.
Output - How to add N working days to the current date in Java?


Similar Problem :


How to add N working days to the given date in Java?

Solution - This is very simple first of all Convert given String to Date and rest is same as above.

References :



Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Date and Time problems, Addition of n working days in Java, Solution in Java, if else statement, for loop, Calendar.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to add N working days to the current date in Java? How to add N working days to the current date in Java? Reviewed by Rohit Agarwal on 1/10/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.