How to get 5 working days from today jira scriptrunner

Hi All, 

 

Im attempting to get 5 working days from issue.getCreated() but im having alot of issues, please see code below: 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.MutableIssue 
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('COM-2479')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
	Date createdDate = issue.getCreated()
   	Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    date = calendar.getTime(); 
   
	//SimpleDateFormat s;
    //s=new SimpleDateFormat("MM/dd/yy");
    int days = 5;
	int i=0;
    for(date;date<createdDate;)
    {
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        if(calendar.get(Calendar.DAY_OF_WEEK)<=5)
        {
            i++;
        }
    }
    //date=calendar.getTime(); 
    //s=new SimpleDateFormat("MMM dd, yyyy");

return createdDate+i

1 answer

1 vote
Vasiliy Zverev Community Champion Mar 09, 2017

Here is code:

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

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('COM-2479')

Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date( issue.getCreated()))
int days = 5;
while (days > 0){
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    if(calendar.get(Calendar.DAY_OF_WEEK) <= 5)
        --days;
}

return calendar.toString();

Thank you so much Vasiliy, ive been tearing my hair out trying to get this to work, it should be so simple lol

i ran the script and threw me some errors: 

image2017-3-10 16:40:6.png

Im using JIRA 6.3.3

 

Many thanks

Vasiliy Zverev Community Champion Mar 09, 2017

This is the reason why I do not like groovy - it does not test on compile time. Here is updated code

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

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('COM-2479')

Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date( issue.getCreated().getTime()))
int days = 5;
while (days > 0){
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    if(calendar.get(Calendar.DAY_OF_WEEK) <= 5)
        --days;
}

return calendar.toString();

Ahhh, got it, thank you so much! all is working!! 

 

thank you for all your help. 

 

 

It's nice solution. Just bear in mind that it's not working in every environment especially be careful with Locales. 

My working sample:

def nextByzDay( Calendar calendar, int days ) { 
while (days > 0) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
if( calendar.get(Calendar.DAY_OF_WEEK) > 1 && calendar.get(Calendar.DAY_OF_WEEK) <= 6)
--days;
}
return calendar
}

You cannot rely on fact Calendar.Sunday is always 7 (Calendar.Sunday==7). For me Calendar.Sunday is 1  (Calendar.Sunday==1). Therefore I suggest to use the constants Calendar.Saturday and Calendar.Sunday.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 14 hours ago in Jira Service Desk

Looking for anyone who has switched from Zendesk to Jira Service Desk

Hi Community! The Jira Service Desk marketing team is looking for customers who have successfully switched from Zendesk to Jira Service Desk!   We’d love to hear your thoughts on the pros and ...

18 views 0 1
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