Scriptrunner Issue Closed Date as Datetime picker

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

This widget could not be displayed.
Vasiliy Zverev Community Champion Dec 06, 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())

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 Community Champion Dec 06, 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)

Nope still get:

$datePickerFormatter.format($value)

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

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Friday in Bitbucket

What is your #1 piece of advice to new users getting started using Bitbucket?

Hello Community! My name is Claire Maynard, and I’m a Product Marketing Manager on the Bitbucket team. I’m interested in hearing what advice or tips you have for new users getting started on ...

125 views 5 2
Join discussion

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