Get DateTime CustomField Value

Rosy Salameh August 11, 2014

Hi Atlassians,

How to get DateTime CustomField Value of a specified issue?

My code is the following:

log.debug("get datetime customField Value: " + issue.getCustomFieldValue(customField_X).toString());

Did I miss something?

Thanks,

Rosy

2 answers

1 vote
Alexey_Rjeutski__Polontech_
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.
August 11, 2014

looks nice. What is customField_X? how do you get that variable? Are you sure there is something inside that issue? can you get other custom field values using that code?

Rosy Salameh August 11, 2014

Hi Alexey,

I got that variable like this:

CustomField customField_X= customFieldManager.getCustomFieldObject(111);

where 111 is the ID of a Date/Time customField. I got the value of the custom field (Text Field) using that code.I should use a specific code for the DateTime field?

Thanks,

Rosy

Alexey_Rjeutski__Polontech_
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.
August 11, 2014

I don't remember I've got some problem with getting of date fields. Could you please modify your code to

log.debug("get datetime customField class: " + issue.getCustomFieldValue(customField_X).getClass().toString());

and give the output generated? Do you see at least the message "get datetime customField Value: "

Rosy Salameh August 12, 2014

Thanks Alexey, you are right, the output is: class java.sql.Timestamp. How should I convert it in order to get the value?

Many thanks,

Rosy

Alexey_Rjeutski__Polontech_
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.
August 12, 2014
Timestamp stamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(stamp.getTime());
System.out.println(date);

http://stackoverflow.com/questions/11839246/how-to-convert-timestamp-to-date-in-java
0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 11, 2014

getCustomFieldValue(customField_X) is going to return the object that is in the field. For many types of object that get stored in fields, .toString() will give you something you can quite easily read and use.

But date and date/time fields return objects (Timestamps, if memory serves) which do NOT implement a .toString() that returns anything that's much use to a human.

You'll need to delve further into the object it does return to work out what you can use. Alexey's code will at least tell you the name of the class you need to work with!

Alexey_Rjeutski__Polontech_
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.
August 11, 2014

As I remember it is Timestamp definitely. But I don't remember that it doesn't implement toString() method (I mean implements empty string) - as for the reference http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Timestamp.html#toString() implementation exists

Rosy Salameh August 12, 2014

Hello again,

I used getValue instead of getCustomFieldValue(customField_X):

log.debug( "customField_X.getValue()" + customField_X.getValue(issue).toString());

and I obtained 2014-07-09 15:49:00.0 for example, now I will try to convert it into the format that I need , but at least, I got the value.

Thanks all,

Rosy

Rosy Salameh August 13, 2014

Hi all,

I sucessfully get the Date/Time Value of the CustomField using the following code:

public String getCustomFieldStringValue(Issue issue, CustomField customField) throws Exception {

String customFieldStringValue = "";

Object value = issue.getCustomFieldValue(customField);

if(value instanceof java.sql.Timestamp) {

DateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");

DateFormat dateFormatWithoutSeconds = new SimpleDateFormat("yyyy-MM-dd HH:mm");

Date date = fullDateFormat.parse(customField.getValue(issue).toString());

customFieldStringValue = dateFormatWithoutSeconds.format(date);

}

log.debug("Custom Field " + customField.getName() + " has the following string value: " + customFieldStringValue);

return customFieldStringValue;

}

But, I'm still facing a problem with the Time Zone.

The value displayed is related to the Time Zone of the Server, however, I should obtain the value related to the Time Zone of the Client. For example, my server is in Paris and I'm using my plugin from Beirut = > The above code gives me the time related to Paris.

How to get the time depending of the time zone of the connected user, not the server where installed Jira?

Thanks,

Rosy

DH March 26, 2021

Hello @Rosy Salameh - perhaps this comes too late :-)

Have you tried:

def userTimeZoneValue = ComponentAccessor.getComponentOfType(TimeZoneManager.class).getLoggedInUserTimeZone().getProperties()['lastRule'].getProperties()['ID'].toString()

 

someDate.format('dd/MMM/yy HH:mm',TimeZone.getTimeZone(userTimeZoneValue))

Suggest an answer

Log in or Sign up to answer