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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

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