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

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

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
Community showcase
Published Thursday in Jira

Updates to jira.atlassian.com give you visibility into what's coming in Jira Server and Data Center

Hello, Community! My name is Gosia and I'm a Product Manager on Jira Server and Data Center here at Atlassian. Since 2002 when we launched our public issue tracker, jira.atlass...

462 views 1 14
Read article

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