Scriptrunner Issue Closed Date as Datetime picker

Chris Shepherd December 6, 2016

I have a scripted field that gets the date an issue  was closed using this code:

import com.atlassian.jira.ComponentManager
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
changeHistoryManager.getChangeItemsForField(issue, "status").find {it.toString == "Closed"}?.getCreated()

 

Which works OK as a text field but I want to use it as a date time field - so it apears with all the other dates on the screen.

 

I just get: 

$datePickerFormatter.format($value)

1 answer

0 votes
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2016

You should perform to steps - convert string to Date formate and then convert date to disirable string format. Here is code:

import com.atlassian.jira.component.ComponentAccessor

import java.text.SimpleDateFormat

return (new SimpleDateFormat("yyyy-MM-dd")) // for desirable format
        .format(new SimpleDateFormat("yyyy/MM/DD")) //for format returned form change history
        .parse(ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, "status").find {it.toString == "Closed"}?.getCreated())
Chris Shepherd December 6, 2016

Thanks for your help but  I just get: 

java.lang.IllegalArgumentException: Cannot format given Object as a Date

 

import com.atlassian.jira.ComponentManager
import java.text.SimpleDateFormat
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
def closed = changeHistoryManager.getChangeItemsForField(issue, "status").find {it.toString = "Closed"}?.getCreated().toString() 
def frmt =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(closed)
return new SimpleDateFormat("yyyy-MM-dd").format(frmt)

 

But that only works as a text field not a date time.

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2016

Try this one:

import com.atlassian.jira.ComponentManager
import java.text.SimpleDateFormat
def componentManager = ComponentManager.getInstance()
def changeHistoryManager = componentManager.getChangeHistoryManager()
String closed = changeHistoryManager.getChangeItemsForField(issue, "status").find {it.toString = "Closed"}?.getCreated().toString()
Date frmt =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(closed)
return new SimpleDateFormat("yyyy-MM-dd").format(frmt)
Chris Shepherd December 6, 2016

Nope still get:

$datePickerFormatter.format($value)

when I try to use it as a datetime picker.
Thanks for the ideas though. 

Gerben Heinen November 1, 2017

I had the same problem. Make sure to set the correct search (a date searcher) on the scripted field. That fixed my problems.

Suggest an answer

Log in or Sign up to answer