How do I subtract the year value of a custom field date (format dd/mm/yyyy).

George November 17, 2017

I am getting this error:

groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1/2/1990' with class 'java.lang.String' to class 'java.util.Date'.

Kindly, help me over come this error.

 

1 answer

0 votes
Joshua Yamdogo @ Adaptavist
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.
November 17, 2017

Is this for ScriptRunner JIRA Cloud or ScriptRunner for JIRA Server?

You're getting that error because you have a "Date" object and you are trying to subtract something that is not actually a "Date" object. Most likely, you need to cast the value you are trying to subtract as a "Date" object. See example script below:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601')

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_11602')

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {
def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2 = issue.getCustomFieldValue(dateFieldObject2) as Date
return dateValue - dateValue2
}
George November 17, 2017

Hi, this is for ScripRunner JIRA Server. 

What I am trying to do here is, from a custom field value in this case date, subtract the year value and show the resulting date as result of another custom field. 

Thank you, I was able to over come the Groovy Cast Exception. I edited the code to what I was trying to achieve, though I am getting a  java.lang.NullPointerException  for this code:

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601')
def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
dateValue.getYear()

Joshua Yamdogo @ Adaptavist
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.
November 17, 2017

Hi George, 

Where are you using this script? In a Scripted Field, post-function, etc?

Looks like you are also missing an import for ComponentAccessor.

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601')
def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
dateValue.getYear()
George November 20, 2017

Hi Joshua,

I am coding this in Scripted Field. 

I do have the import com.atlassian.jira.component.ComponentAccessor

It still gives the same error.

 

The dateValue is supposed to contain : 1/4/2000 (format: dd/mm/yyyy)

But instead when I check to see if it s returning the above date, before I do the dateValue.getYear(), it gives me the error  java.lang.NullPointerException

I am guessing this is because the code did not read the dateValue from my JIRA issue.

on the other hand when i use this code:

I am getting this error for the below code:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1/4/2000' with class 'java.lang.String' to class 'java.util.Date'


def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateField = customFieldManager.getCustomFieldObjectByName('customfield_11')

if(issue.getCustomFieldValue(dateField)) {
def dateValue = issue.getCustomFieldValue(dateField) as Date

return dateValue.getYear()
}

 

 

 

-- When I remove the 'as date' return type from def def dateValue = issue.getCustomFieldValue(dateField), this is the error I get :

 

groovy.lang.MissingMethodException: No signature of method: java.lang.String.getYear() is applicable for argument types: () values: [] Possible solutions: getChars(), getChars(int, int, [C, int), getAt(groovy.lang.Range), getAt(groovy.lang.IntRange), getAt(groovy.lang.Range), getAt(int)

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateField = customFieldManager.getCustomFieldObjectByName('customeField_11')

if(issue.getCustomFieldValue(dateField)) {
def dateValue = issue.getCustomFieldValue(dateField)

return dateValue.getYear()
}

 

George November 20, 2017

I found this below line of code to convert string to date in JIRA docs

{{issue.summary.toDate("yyyy mm dd")}}

but I am not sure how to add this in my code, I am still trying to figure this out. 

Joshua Yamdogo @ Adaptavist
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.
November 20, 2017

George,

Before we can fix the date calculation, we need to fix other parts of your script.

In your script below, it seems there is an error with getting the field value. You are calling getCustomFieldObjectByName('customField_11').

Surely, that is not the real name of the field, is it? What is the actual name of the field as it appears on the issue?

'customfield_11' seems like a field ID, in which case you would need to simply call getCustomFieldObject('customfield_11').

George November 20, 2017

Yes, that was just an example value.

The actual field name is:

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def fieldName = customFieldManager.getCustomFieldObject('Upcoming Fiscal Year Date')

def dateValue = issue.getCustomFieldValue(fieldName) as Date

def yearValue = dateValue.getYear()

Joshua Yamdogo @ Adaptavist
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.
November 20, 2017

Hi George,

Thanks for clarifying that. OK, so we just need to figure out how to calculate the correct date. It seems like it would be simple, but it is tricky sometimes.

Could you please answer these questions as well:

1) What type of custom field is 'Upcoming Fiscal Year Date'? Is it a Date Picker, a Date Time Picker, or a Text field? You can view this on the manage custom fields page.

2) What template is your Script Field using?

Screen Shot 2017-11-20 at 2.27.00 PM.png

George November 20, 2017

I have used the Text field template for the custom field 'Upcoming Fiscal Year Date'. 

 

text template.PNG

The custom field above was meant to perform some calculations in which I used a Date time picker custom field (not Scripted Field) called  'User Specified Date_Custom Field", for manual date entry to provide the input date to perform these calculations and generate the output for the 'Upcoming Fiscal Year Date' custom field.

 

Date Picker Custom Field.PNG

This was to test the correct working of the calculations in all possible scenarios.

I would be changing this Date Picker Custom field called 'User Specified Date_Custom Field" and use the System Date" as the input for my 'Upcoming Fiscal Year Date' Scripted field. 

George November 21, 2017

 I tried changing the template to Date Time Picker and Absolute Date Time and I am getting the result as :

$datePickerFormatter.format($value)

Result.PNG 

Date Picker

template2.PNG

Absolute Date Time

Template1.PNG

Joshua Yamdogo @ Adaptavist
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.
November 21, 2017

Thanks for that information George. So in short, you were previously taking information from a custom field that allowed the user to specify a date manually. From that user-specified field, your scripted field was calculating a date.

Now, you want to use the System Date instead of the user-specified field. Using the JIRA System Date as input, you want to subtract years from it and display the result in your scripted field. Is that correct?

George November 21, 2017

Yes, that is correct and I need the Date that is displayed to be in Date format and not a String, so I will need to use the Date Template. Though at this point, I am not sure which one would be suitable Absolute Date or Date Time Picker. 

If you see the images I posted both these templates where giving me the result as $datePickerFormatter.format($value)

George November 27, 2017

Hi Joshua,

I got my code working with Date Time Picker. I set the searcher as Date Time Picker and the Template as Date Time.

import com.atlassian.jira.component.ComponentAccessor;
import java.util.Date
import java.text.SimpleDateFormat
import java.text.DateFormat
import java.text.ParseException
import java.util.*
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.lang.Object

//get the user specified date store it in variable start
def userDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("User Specified Date_Custom Field")
def start = issue.getCustomFieldValue(userDate) as Date


//System Date
//Date start = new Date()

//Specify the date format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

//Curret Year
def year = 1900 + start.getYear()
//Increament the Year value by 1
def cYear = year + 1
//Current Month
def cMonth = start.getMonth() + 1
//Current Day
def cDay = start.getDay()
//Month and Day
def fMonth = 4
def fDate = 1


if (cMonth == 1) // If current month is January
{
String nxtFYr = fDate + '/' + fMonth + '/' + year ;
Date upcomingYearDate = sdf.parse(nxtFYr);
}
else if (cMonth == 2) // If current month is February
{
String nxtFYr = fDate + '/' + fMonth + '/' + year ;
Date upcomingYearDate = sdf.parse(nxtFYr);
}
else if (cMonth == 3) // If current month is March
{
String nxtFYr = fDate + '/' + fMonth + '/' + year ;
Date upcomingYearDate = sdf.parse(nxtFYr);
}
else if ( fMonth - cMonth < 0) // If month minus Current month is less than 0
{
String nxtFYr = fDate + '/' + fMonth + '/' + cYear
Date upcomingYearDate = sdf.parse(nxtFYr);
}
else if (fDate - cDay <= 0) // If day minus Current day is less than or equal to 0
{
String nxtFYr = fDate + '/' + fMonth + '/' + cYear
Date upcomingYearDate = sdf.parse(nxtFYr);
}
else print "NA"

 

The Issue I am facing now is that, This is displaying the Time along with the Date, I would like to remove the Time Value. Also, I am trying to set the upcomingYearDate the output of this custom field in my confluence calendar as an reminder but this Date Time Picker custom field is not reflecting in the list. The User Specified Date_Custom Field which is a Date Time Picker(not scripted) is reflecting there. I am guessing this is to do with the time value appended to the Date. Please correct me if I am wrong.

result1.PNG

Like Azfar Masut likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events