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 Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

807 views 3 9
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