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

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.

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 Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,196 views 6 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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot