The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Issue Closed Date as Datetime picker

Chris Shepherd
Contributor
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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

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 Champions.
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
Contributor
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 Champions.
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
Contributor
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
Contributor
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.