Calculate working days between 2 dates

Hi Guys, 

 

Im trying to calculate 2 working days in postfunction but im having a abit of trouble, here is the code i currently have: 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue 
import com.atlassian.jira.issue.CustomFieldManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('OE-2250')

Date test = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10804)); //09-Jan-17
Date test2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11504)); //16-Jan-17

startCal = Calendar.getInstance();
endCal = Calendar.getInstance();
startCal.setTime(test);   
endCal.setTime(test2);   
int numberOfDays = 0;
while (startCal.before(endCal)) {
            if ((Calendar.SATURDAY != startCal.get(Calendar.DAY_OF_WEEK))&&(Calendar.SUNDAY != startCal.get(Calendar.DAY_OF_WEEK))) {
                numberOfDays++;
                startCal.add(Calendar.DATE,1);
            }else {
                startCal.add(Calendar.DATE,1);
            }
        }
		return numberOfDays

 

Thank you so much

1 answer

This widget could not be displayed.
Vasiliy Zverev Community Champion Jan 09, 2017

Try this refactored code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('OE-2250')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();

startCal.setTime(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10804)));    //09-Jan-17
endCal.setTime(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11504)));      //16-Jan-17
int numberOfDays = 0;

while (startCal.before(teendCalst2)) {
        startCal.add(Calendar.DAY_OF_YEAR, 1);
        if (    (Calendar.SATURDAY != startCal.get(Calendar.DAY_OF_WEEK))
              &&(Calendar.SUNDAY != startCal.get(Calendar.DAY_OF_WEEK))) {
                numberOfDays++;
        }
}

return numberOfDays;

Thank you so much again Vasiliy, definielty does work, but its looks like its calculating Sunday into the logic: 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('OE-2264')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();
startCal.setTime(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10804)));    //14-Jan-17
endCal.setTime(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11504)));      //18-Jan-17
int numberOfDays = 0;
while (startCal.before(endCal)) {
        startCal.add(Calendar.DAY_OF_YEAR, 1);
        if ((startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) && (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY)) {
                numberOfDays++;
        }
}
return numberOfDays

 Is returning 4

 

Any idea why this is happening?

 

Cheers,

Pon

Vasiliy Zverev Community Champion Jan 09, 2017

Try this one:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('OE-2264')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Calendar startCal = Calendar.getInstance();
Calendar endCal = Calendar.getInstance();
startCal.setTime(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10804)));    //14-Jan-17
endCal.setTime(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11504)));      //18-Jan-17
int numberOfDays = 0;
while (startCal.before(endCal)) {
        startCal.add(Calendar.DAY_OF_YEAR, 1);
        if (    (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) 
             || (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY)) {
                continue;
        }
        numberOfDays++;
}
return numberOfDays

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 10 hours ago in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

32 views 1 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you