Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Groovy how to substract days from given date

Omprakash Thamsetty
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.
September 18, 2019

Hi,

 

I have a target date, which I needs to reduce 2 days during the transition. How I can do it using postfunction.

My target date 10/1/2019 so I would like to set 9/29/2019.

 

I am getting the value of target date by

def TD = customFieldManager.getCustomFieldObjectByName("Target Date")
def TDV = issue.getCustomFieldValue(TD)

but unable to subtract it. How I can do that? Any advise.

Thanks,

Om

1 answer

0 votes
Ilya Turov
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.
September 18, 2019

try something like this, iirc you can just subtract a number from timestamp and groovy will understand

last piece is for updating issue, though if it's a postfunction last line is not necessary

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def TD = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Target Date")
def TDV = issue.getCustomFieldValue(TD)
TDV -= 2
issue.setCustomFieldValue(TD, TDV)
ComponentAccessor.issueManager.unpdateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
Omprakash Thamsetty
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.
September 18, 2019

@Ilya TurovIt doesn't work but I did by converting to date and string as below.

 

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser


def TD = customFieldManager.getCustomFieldObjectByName("Target Date")
def TDV = issue.getCustomFieldValue(TD)

Date newTDV = (Date)TDV
def newDateformat = newTDV.format('MM/dd/yyyy')
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

cal.setTime(sdf.parse(newDateformat));
cal.add(Calendar.DATE, -2);

Timestamp DefTDD = new Timestamp(cal.getTimeInMillis())

issue.setCustomFieldValue(TD, DefTDD)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED ,false)


Ilya Turov
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.
September 19, 2019

I made a typo, should be 

issueManager.updateIssue(...

not unpdateIssue, but anyway, if this postfunction is first in list (before the one that stores issue to db) you don't need to manually update it

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events