add (calender days) to date using script runner

A November 5, 2015

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
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2015

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

A November 6, 2015

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

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 6, 2015

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

A November 6, 2015

Thanks a ton..for the code and prompt response

Suggest an answer

Log in or Sign up to answer