Hi,
Im using script field to get date field from another linked issue.
I would like to edit the date format to retun dd/MMM/yy instead of dd/MMM/yy hh:mm a
Can you please advise?
import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.link.IssueLink; import java.text.SimpleDateFormat; import java.text.DateFormat def readCustomFieldValue (String customFieldName, Issue issue) { CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager(); def customField = customFieldManager.getCustomFieldObjectByName(customFieldName) if (customField != null) { return issue.getCustomFieldValue(customField) } } //Issue issue = ComponentAccessor.getIssueManager().getIssueObject("SWMOCKUP-13"); for (IssueLink link in ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())) { if (link.getDestinationObject().getIssueTypeObject().name.contains('SW EE Activity')) { def datetime = readCustomFieldValue("Program Req. Expected Date",link.getDestinationObject()).time.toString() long milliSeconds= Long.parseLong(datetime); DateFormat formatter = new SimpleDateFormat("dd/MMM/yy"); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(milliSeconds); return formatter.format(calendar.getTime()); } }
Community moderators have prevented the ability to post new answers.
issue.getCustomFieldValue returns a java.sql.Timestamp object for date & time fields. TimeStamp extends Date, so you get all the built-in Groovy convenience methods for free.
If your script field is using the Free Text Searcher and a text-based template, then you should be able to do something as simple as
for (IssueLink link in ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())) { if (link.getDestinationObject().getIssueTypeObject().name.contains('SW EE Activity')) { def datetime = readCustomFieldValue("Program Req. Expected Date",link.getDestinationObject()) as Timestamp return datetime.format("dd/MMM/yy") } }
However, you're probably better off using the Date Time Range Picker searcher for your custom field so that you can search on it. If the Date Time Picker template is leaving those pesky hours on there, there's a couple of things I'd try.
First, just use Groovy's .clearTime() method.
for (IssueLink link in ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())) { if (link.getDestinationObject().getIssueTypeObject().name.contains('SW EE Activity')) { def datetime = readCustomFieldValue("Program Req. Expected Date",link.getDestinationObject()) as Timestamp return datetime.clearTime().format("dd/MMM/yy") } }
If the time is still showing up as midnight, then you may need to use a custom velocity template. Something like
<time class="livestamp allow-future">$dateFieldFormat.format($value)<time>
You can read more about what's available in the binding for custom templates in the docs.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.