How to subtract N working days from the current date in Java?
Problem Description :
Write a program in Java that subtracts N working days from the current date and prints new date with proper validations.
Concept :
There are 5 working days per week (Monday, Tuesday, Wednesday, Thursday, Friday). Here in a loop we are subtracting 1 from 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 subtraction from current date using add() method of Calendar class that accepts DAY_OF_MONTH constant and number of days that you want to subtract in negative format.
- For getting day, we need to use DAY_OF_WEEK constant of Calendar class.
Recommended :
How to how to find every information about given date?
Java program :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex.datetime; | |
import java.util.Calendar; | |
import java.util.Scanner; | |
/** | |
* | |
* @author Rohit Agarwal | |
* @category Date and Time | |
* @problem How to subtract N working days from current date? | |
* | |
*/ | |
public class SubtractingNWorkingDays { | |
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++) { | |
// Subtracting 1 day from 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("Before " + orignalLimit + " working day/s Date : " + calendar.getTime()); | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
} | |
} | |
} |
Output :
![]() |
Output - How to subtract N working days from current date in Java? |
Similar Problem :
How to subtract N working days from 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, Subtraction of n working days in Java, Solution in Java, if else statement, for loop, Calendar.
Tags : Date and Time problems, Subtraction of n working days in Java, Solution in Java, if else statement, for loop, Calendar.
How to subtract N working days from the current date in Java?
Reviewed by Rohit Agarwal
on
1/16/2017
Rating:

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.