It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Can't get current date in custom field

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

Hi @Andrei Lavrinovich .

Try this.

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

issue.setCustomFieldValue(Current_Date, date)

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?

@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
0 votes
tom lister Community Leader Dec 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

"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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you