It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Calculate working days between 2 dates

Pon Yuntawai Jan 08, 2017

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 votes
Vasiliy Zverev 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;
Pon Yuntawai Jan 09, 2017

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 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

2,684 views 6 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you