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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot