Can't get current date in custom field

Andrei Lavrinovich December 23, 2019

Hi, i have a script that must give me a current time, but instead of this it gives me java.lang.NullPointerException. 

 

script is here:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import java.sql.Timestamp

import static java.lang.Math.*

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("SEREX-2")

def Current_Date= ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Current date")

// get today's date

issue.setCustomFieldValue(Current_Date, new Timestamp((new Date() + 1).time))

2 answers

1 accepted

0 votes
Answer accepted
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 23, 2019

Hi @Andrei Lavrinovich .

Try this.

import java.text.SimpleDateFormat
Date date = new SimpleDateFormat('yyyyMMdd')

issue.setCustomFieldValue(Current_Date, date)
Andrei Lavrinovich December 23, 2019

I have found something more easily

return new Date()

that give me current date but only in "Text Field" template and not in "Date Time" template. Do you know maybe something similar for date time template?

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 23, 2019

@Andrei Lavrinovich 

Here is the working code.

import java.text.SimpleDateFormat
SimpleDateFormat date1 = new SimpleDateFormat('dd/MMM/yy') //24/Dec/19
SimpleDateFormat date2 = new SimpleDateFormat('dd/MMM/yy h:m a') //24/Dec/19 6:56 AM

def format1 = date1.format(new Date())
def format2 = date2.format(new Date())

log.debug(format1)
log.debug(format2)

It only works if the Customfield Date format is dd/MMM/yy .e.g 24/Dec/19

If the Customfield is DateTime then the format should be dd/MM/yy h:m a e.g 24/Dec/19 6:56 AM

 

Refer to this page for more information.

https://www.journaldev.com/17899/java-simpledateformat-java-date-format

Like Andrei Lavrinovich likes this
Andrei Lavrinovich December 24, 2019

Untitled.png

 

Untitled.png

0 votes
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 23, 2019

Hi @Andrei Lavrinovich 

My first point of call with a null pointer exception is to add logging and identify what is null

import org.apache.log4j.Loggerimport java.util.Date 
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
...

log.info("" + Current_Date)
etc

chances are one of your access has not returned an object

Andrei Lavrinovich December 23, 2019

"return new Date()" give me current date and time, but not in date time template

Suggest an answer

Log in or Sign up to answer