Our new Appfire Documentation Space is now live!
Take a look here! If you have any questions please email support@appfire.com
Add a certain number of days to a date excluding the weekends
Abstract
This code snippet adds a certain number of days to a date excluding the weekends. By default, when you add a certain number of days to a date the weekends are included. To exclude them, you can use this snippet.
Logic
- Fetch the number of days to be added and first add whole weeks (of 5 work days)
- Run a loop to add a day each until the number of remaining days - Skip if the day is a "Saturday" or a "Sunday"
- After adding the days, skip the end if it is a weekend
- Clear the time and return the date
Snippet
Date date(Date from, int nod){ Calendar c1 = GregorianCalendar.getInstance(); c1.setTime(from); int weeks = nod/5; int remDays = nod%5; //Adding whole weeks c1.add(Calendar.WEEK_OF_YEAR, weeks); //Run a loop and check each day to skip a weekend for(int i=1;i<=remDays;i++){ c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); } //Skip ending weekend if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); //move to 0:00 c1.clearTime(); return c1.getTime(); } date(<from>,<nod>)
Placeholders
Placeholder | Description | Example |
---|---|---|
<from > | Date to which the number of days should be added | new Date() |
<nod> | Number of days to add | 22 |
Examples
The output of this code snippet is a Timestamp which you could use in:
A Groovy expression, for example to:
- Set a Date/Date-time picker field - Eg: Set a Date-time picker field to 5 days plus the Due date in
- one of the Set Field Value post-functions
the Create issue post-function under Set fields of new issue section
Date date(Date from, int nod){ Calendar c1 = GregorianCalendar.getInstance(); c1.setTime(from); int weeks = nod/5; int remDays = nod%5; //Adding whole weeks c1.add(Calendar.WEEK_OF_YEAR, weeks); //Run a loop and check each day to skip a weekend for(int i=1;i<=remDays;i++){ c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); } //Skip ending weekend if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); //move to 0:00 c1.clearTime(); return c1.getTime(); } date(issue.get("duedate"),5)
A Groovy template, for example to:
- Notify the customer that the issue will be resolved in ten days excluding weekends, through the
- Comment in one of the Comment issue post-functions
Subject/HTML body/Text body of Email issue post-function
<% Date date(Date from, int nod){ Calendar c1 = GregorianCalendar.getInstance(); c1.setTime(from); int weeks = nod/5; int remDays = nod%5; //Adding whole weeks c1.add(Calendar.WEEK_OF_YEAR, weeks); //Run a loop and check each day to skip a weekend for(int i=1;i<=remDays;i++){ c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); } //Skip ending weekend if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); //move to 0:00 c1.clearTime(); return c1.getTime(); }%> Your issue will be resolved on or before <%=date(new Date(),10)%>
References
Related articles