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

How to convert an indexed date back to a Date in Jira 8

I'm setting a due date on an issue, which is now stored as a numeric string. For instance, setting a due date to December 19, 2018 on an issue gives me "17884" when reading the DocumentConstants.ISSUE_DUEDATE field of that issue from the index. How do I convert that string/number back to a Date?

 

 

 

1 answer

Apparently, that number is a number of days since Jan 1st, 1970.  So a dateString coming from Lucene is converted to a Date by

new java.util.Date(Long.parseLong(dateString) * 24 * 60 * 60 * 1000);

 in JIRA 8.

Seth Utecht Atlassian Team Jan 16, 2019

Hey Marc-André,

Quick question, where are you getting the date string from? I ask because In the Lucene index itself, dates are now being stored as longs. 

But you're right on the money with the offset; we store due dates as the number of days since the unix/java epoch on Jan. 1st, 1970. Also, if you're able to make use of Java's new Date/Time api, 

LocalDate.ofEpochDay(daysSinceEpoch)

makes the conversion a little easier.

Just a word of caution: for other Jira fields that capture both a date and a time, the number stored in the Lucene index is the number of milliseconds since midnight on the epoch.

Best,
Seth

Thanks for your reply @Seth Utecht!

We're getting string values because we're reading them from the index using e.g.

org.apache.lucene.document.Document.getValues("duedate")

which returns an array of Strings. So a date value read that way is in fact a Long represented as a String (e.g. "17884").

Thanks for confirming our assumption about the date representation!  We also noticed what you mentioned about date and time values (like the `resolutiondate` field for instance) being formatted as a number of milliseconds. Does the jira api provide some utility method to safely parse a date OR date and time to a Date without the code having to know what's actually stored in a given index field?  Though we could just as well implement this by guessing based on the Long value length...

Seth Utecht Atlassian Team Jan 17, 2019

Hey @Marc-André Thibodeau,

Thanks for the clarification on the string values! 

And I'm afraid there's not a utility in the Jira api for parsing a date or date+time without knowing the underlying use of the field. 

Best,
Seth

Any help for doing this on Jira 7? I'm trying to read the "created" date but instead of getting a String I'm getting non-printable characters:

Selection_275.png

It seems like dates are stored as some kind of binary format. Do you know how can I convert this back to java.util.Date?

I found the answer here@Peter Runge says

"JIRA stores dates in Lucene as the number of seconds since the Epoch (1 January 1970) in Lucene long prefix-coded format." so you can either:

String lucenePrefixedCoded = document.get(DocumentConstants.ISSUE_CREATED);
long secondsSinceEpoch = NumericUtils.prefixCodedToLong(lucenePrefixedCoded);
Date created = new Date(secondsSinceEpoch * 1000);

or the shortcut:

Date created = LuceneUtils.stringToDate(document.get(DocumentConstants.ISSUE_CREATED));

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

838 views 5 22
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