Scriptrunner date parse

In scriptrunner console i am trying to parse date from String. The server outputs me: Wed Apr 05 16:41:00 EEST 2017

Purpose: Put sysdate value to custom date time field using scriptrunner post functcion.

I need: 2017/04/05 16:41 or 2017.04.05 16:41

import org.apache.log4j.Category;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
def cal1 = Calendar.getInstance();
String format = "yyyy/MM/dd HH:mm";
SimpleDateFormat sdfSource = new SimpleDateFormat(format);
String formatedDate = sdfSource.format(cal1.getTime());
formatedDate
//new Date().parse("yyyy/M/d HH:mm", formatedDate)

Date settings:

image.png

2 answers

0 vote

You can do this with the following code in a custom scripted post-function:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

import java.sql.Timestamp

MutableIssue issue = issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// replace with your custom field id
def customFieldId = "customfield_10028"
def customField = customFieldManager.getCustomFieldObject(customFieldId)

def todaysDateTime = new Timestamp((new Date()).time)

issue.setCustomFieldValue(customField, todaysDateTime)

You will need to change the custom field id above to match your date time field. The post-function also needs to be in the list before the change history one. This is explained in more detail here.

Let us know how you get on with this.

Hello,

 

Many thanks to your answer. It worked. I modified the code a little.

import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import groovy.time.*
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import org.apache.log4j.Category;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue
def log = Logger.getLogger("zyglev")
log.setLevel(Level.DEBUG)
MutableIssue issue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Start date"}
def customFieldValue = issue.getCustomFieldValue(customField)
if (customFieldValue)
{
return 0
}
else
{
def todaysDateTime = new Timestamp((new Date()).time)
issue.setCustomFieldValue(customField, todaysDateTime)
}

Žygimantas

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
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

364 views 0 6
Read article

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