Forums

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

Change format of Date in JIRA Post Function Script

Wayne Cranford March 13, 2018

Need to change the Date output when using tgtField.updateValue in a JIRA Post Function script.

The default output is burdensome to my management as it returns "Tue Mar 13 14:03:52 CDT 2018" and they are requesting to change to a DD/MM/YY format.

 I currently have the following with respect to the date:
Date date = new Date();

tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + date + field2Value + field3Value  + field4Value + field7Value + field5Value),changeHolder);

I have unsuccessfully attempted using the  "SimpleDateFormat" from other examples  I found while searching (see below for one), but nothing seemed to work as the output would either return the extended format or "java.text.SimpleDateFormat@f82ede60".

I tried working on the below over 2 hours, but it has if statements that I am not in need of, and could not extract what was needed for the simpler format.

https://community.atlassian.com/t5/Jira-Core-questions/Posting-a-script-runner-date-result-to-another-date-customfield/qaq-p/150289

All feedback appreciated.

1 answer

0 votes
Alexey Matveev
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.
March 13, 2018

If you want to represent your date in DD/MM/YY then you code should look like this

Date date = new Date();

SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/YY");

log.error(dateFormat.format( date ) );

Wayne Cranford March 14, 2018

Alexis,

 

Thank you for your feedback, and my apologies on my delay– I did not receive the notification of your response.

I added the 2nd and 3rd line you suggested (already had the 1st line) to my script and still did not receive the desired date format into the custom field.  I am making other attempts, but here is what I received only adding those two changes:

Wed Mar 14 10:36:51 CDT 2018 : Temp [Resume Assembly] for 1245678, Sales Order 2, QTY: 1 units

Here is the complete script that I am using, but note that in this attempt I did not change the “date” in the tgtField.updateValue at the bottom, which is what is creating the entry into the custom field:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat;

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/YY");
log.error(dateFormat.format( date ) );
IssueManager issueManager = ComponentAccessor.getIssueManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Type"}
CustomField field3 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Resume Temp P/N(s)"}
CustomField field4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume For"}
CustomField field5 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Qty"}
CustomField field6 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Special Instructions"}
CustomField field7 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Ref Order or S/N #"}
def checkvalid = issue.getCustomFieldValue(field6)

String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = " : Temp " + (String)issue.getCustomFieldValue(field2)
String field3Value = " for " + (String)issue.getCustomFieldValue(field3)
String field4Value = ", " + (String)issue.getCustomFieldValue(field4)
String field5Value = ", QTY: " + (String)issue.getCustomFieldValue(field5) + " units"
String field6Value = ", Spcl Instructions: " + (String)issue.getCustomFieldValue(field6)
String field7Value = " " + (String)issue.getCustomFieldValue(field7)
String field8Value = ""
String lineSeparator = System.getProperty("line.separator")

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
def changeHolder = new DefaultIssueChangeHolder();

if (checkvalid == null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + date + field2Value + field3Value + field4Value + field7Value + field5Value),changeHolder);
}
if (checkvalid != null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + date + field2Value + field3Value + field4Value + field7Value + field5Value + field6Value),changeHolder);
}

 

 

For my 2nd attempt I replaced “date” in the tgtField.updateValue lines with “dateFormat” and received the following:

 java.text.SimpleDateFormat@f98c3ba0 : Temp [Resume Assembly] for 1187501G1, Work Order 465789, QTY: 200 units, Spcl Instructions: Testing Alexey's recommendation on the date format.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat;

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/YY");
log.error(dateFormat.format( date ) );
IssueManager issueManager = ComponentAccessor.getIssueManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Type"}
CustomField field3 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Resume Temp P/N(s)"}
CustomField field4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume For"}
CustomField field5 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Qty"}
CustomField field6 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Special Instructions"}
CustomField field7 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Ref Order or S/N #"}
def checkvalid = issue.getCustomFieldValue(field6)

String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = " : Temp " + (String)issue.getCustomFieldValue(field2)
String field3Value = " for " + (String)issue.getCustomFieldValue(field3)
String field4Value = ", " + (String)issue.getCustomFieldValue(field4)
String field5Value = ", QTY: " + (String)issue.getCustomFieldValue(field5) + " units"
String field6Value = ", Spcl Instructions: " + (String)issue.getCustomFieldValue(field6)
String field7Value = " " + (String)issue.getCustomFieldValue(field7)
String field8Value = ""
String lineSeparator = System.getProperty("line.separator")

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
def changeHolder = new DefaultIssueChangeHolder();

if (checkvalid == null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + dateFormat + field2Value + field3Value + field4Value + field7Value + field5Value),changeHolder);
}
if (checkvalid != null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + dateFormat + field2Value + field3Value + field4Value + field7Value + field5Value + field6Value),changeHolder);
}

                                         

Alexey Matveev
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.
March 14, 2018
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat;

Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/YY");
String formattedDate = dateFormat.format( date );
IssueManager issueManager = ComponentAccessor.getIssueManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Type"}
CustomField field3 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Resume Temp P/N(s)"}
CustomField field4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume For"}
CustomField field5 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Qty"}
CustomField field6 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Special Instructions"}
CustomField field7 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Ref Order or S/N #"}
def checkvalid = issue.getCustomFieldValue(field6)

String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = " : Temp " + (String)issue.getCustomFieldValue(field2)
String field3Value = " for " + (String)issue.getCustomFieldValue(field3)
String field4Value = ", " + (String)issue.getCustomFieldValue(field4)
String field5Value = ", QTY: " + (String)issue.getCustomFieldValue(field5) + " units"
String field6Value = ", Spcl Instructions: " + (String)issue.getCustomFieldValue(field6)
String field7Value = " " + (String)issue.getCustomFieldValue(field7)
String field8Value = ""
String lineSeparator = System.getProperty("line.separator")

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
def changeHolder = new DefaultIssueChangeHolder();

if (checkvalid == null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + formattedDate + field2Value + field3Value + field4Value + field7Value + field5Value),changeHolder);
}
if (checkvalid != null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + lineSeparator + field8Value + formattedDate + field2Value + field3Value + field4Value + field7Value + field5Value + field6Value),changeHolder);
}
Wayne Cranford March 14, 2018

Alexey,

Thank you that resolved my issue. In hindsight the need for the string makes sense as all of the other fields I am using included these. 

Best regards,,
Wayne 

Wayne Cranford March 14, 2018

As follow-up, I did change the day from "DD" to "dd" 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events