add (calender days) to date using script runner

I am trying to create custom scripted field using script runner to calculate the end dates

But got stuck here.

below is my code

import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp;
 
MutableIssue myIssue = issue;
def dateField = getCustomfieldValue("Date")
Calendar dateField = Calendar.getInstance()
def numberfield = getcustomFieldValue("Days")
def number = numberfield?.intValue()

if number (!number || !dateField)
return
    
dateField.add(Calendar.DATE, number)
while ([Calendar.SATURDAY, Calendar.SUNDAY].contains(dateField.get(Calendar.DAY_OF_WEEK))) {
    def dateField.add(Calendar.DATE,number)
}
def result =  new Timestamp(dateField.gettime())
return new Date(result.gettime())

1 answer

1 accepted

2 votes
Accepted answer

Hey Amar,

I thought that what you wanted was related to your last question here (https://answers.atlassian.com/questions/32023338), if not please let me know in order to delete it. You can try this part of code (I left in purpose the variable types). Of course there is room for optimisation.

import java.sql.Timestamp

Timestamp dateField = (Timestamp) getCustomFieldValue("DateCF")
long numberField = (long) getCustomFieldValue("NumberCF")

if (numberField == null)
    return

int number = numberField.intValue()

if (dateField) {
    Date result = dateField;
    Date newDate = new Date(result.getTime())
    int i = 0 ;
    while (i < number) {

        result = result + 1
        newDate = new Date(result.getTime())
        if (newDate[Calendar.DAY_OF_WEEK] == Calendar.SATURDAY || 		 newDate[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY) {
            number ++;
        }

        i ++;
    }
    return newDate
}

return

Regards,

Thanos

Is there a manual.. or a reference guide where i can learn this... Because every time getting stuck with new problem

Amar, I suppose what you mean is to learn how to write scripts. There is plenty of documentation in the web. Especially for the script runner there are a lot of examples in the documentation https://scriptrunner.adaptavist.com/latest/jira/quickstart.html. What I can advise you is to keep practicing. Regards, Thanos

Thanks a ton..for the code and prompt response

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,512 views 15 20
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