date format for date time searcher

Paz Grimberg February 22, 2017

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()); 
    }
}

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Jonny Carter
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.
February 23, 2017

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.

Paz Grimberg February 25, 2017

Thanks Jonny! ill try it.

TAGS
AUG Leaders

Atlassian Community Events