Forums

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

Parse date from string in post-function

Steve Beauchamp March 1, 2021

I am trying to compare two dates to each other

  • Current date/time
  • FixVersion start date +16 hours

How I am doing this is:

//To get the actual DATE
def today = new Date().format('yyyy-MM-dd hh:mm:ss')

//To define the Done DATE and TIME
def done = issue.get("fixVersions").startDate.toString()


if ((today) < (done)) {

true
}
else{false}

 This technically works to compare the dates, but if I want to add time to the FixVersion start date, I need to convert the string to a date object. I have tried to do that by changing the code to:

//To define the done DATE and TIME
def done = issue.get("fixVersions").startDate.toString()

def doneDate = new Date().parse("yyyy-MM-dd hh:mm:ss", done)

However, this fails with the following error:

 

Message:
java.text.ParseException: Unparseable date: "[2021-02-28 00:00:00.0]"

Any idea what I'm doing wrong here?

 

1 answer

1 accepted

2 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
March 1, 2021

Hi @Steve Beauchamp , I think "fix versions" is Collection of values (https://docs.atlassian.com/software/jira/docs/api/8.12.0/com/atlassian/jira/issue/Issue.html#getFixVersions--), so what groovy does is:

  1. take ALL fix versions and even if there is only one value set, issue.get("fixVersions") returns Collection
  2. takes startDate property of the objects in the collection
  3. invokes toString() on collection of start dates

So you probably need something like, because startDate is of Date type (https://docs.atlassian.com/software/jira/docs/api/8.12.0/com/atlassian/jira/project/version/Version.html#getStartDate--):

  • def doneDate = issue.getFixVersions().get(0)?.startDate
Steve Beauchamp March 1, 2021

Thanks! This worked perfectly. I just had to modify it slightly to compare the date + 16h to 'today'.

The final code I used was:

//To get the actual DATE
def today = new Date().format('yyyy-MM-dd hh:mm:ss')

//To define the Done DATE and TIME

def doneDate = issue.getFixVersions().get(0)?.startDate.format('yyyy-MM-dd')+" 16:00:00"


if ((today) < (doneDate)) {

true
}
else{false}
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
March 1, 2021

@Steve Beauchamp  you are welcome :) you can simplify code:

return today < doneDate
Like Steve Beauchamp likes this

Suggest an answer

Log in or Sign up to answer