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

Get DateTime CustomField Value

Rosy Salameh Aug 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
Aleksei Rjeutski Aug 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 Aug 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

Aleksei Rjeutski Aug 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 Aug 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

Aleksei Rjeutski Aug 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 Aug 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!

Aleksei Rjeutski Aug 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 Aug 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 Aug 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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

838 views 0 12
Read article

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