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

This widget could not be displayed.

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.

This widget could not be displayed.

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 Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

539 views 3 19
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