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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

113 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