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

0 vote
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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Meet the AUG leaders of Northern Virginia

@Rachel Wright (Jira Genie), @Billy Poggi (AUG NOVA, DC), and @Dana Jansen (Confluence Queen) are just some of the folks that lead one of the world's most active Atlassian User Group (AUG)....

154 views 5 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