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 votes

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
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

412 views 0 3
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