Get DateTime CustomField Value

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

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?

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

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

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

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

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!

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

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

467 views 7 5
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you