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 :
- 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. - 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. - A
java.util.Calendar
object can be retrieved by calling its staticgetInstance()
method. Overloaded variants of this method take timezone and locale but if calendar is created using no-arggetInstance()
method then it is created using the default timezone and locale of the user’s system. getTime()
method ofjava.util.Calendar
class returns the date object representing the calendar’s object time value.add()
method ofjava.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.- Both arguments of
java.util.Calendar
‘sadd()
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 typeint
and each is assigned a value.
So, in place ofCalendar.MONTH
toadd()
method you can pass 2 sinceMONTH
is assigned a value of 2. format()
method, which takes ajava.util.Date
object as argument, called onjava.text.SimpleDateFormat
object converts a date into String format. This method belongs tojava.text.DateFormat
class which is the parent ofjava.text.SimpleDateFormat
.parse()
method which takes a String also belongs tojava.text.DateFormat
class. It may throw ajava.text.ParseException
so you need to catch it in the method whereparse()
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.