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

Marc-André Thibodeau December 18, 2018

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

1 vote
Marc-André Thibodeau December 18, 2018

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
Atlassian Team members are employees working across the company in a wide variety of roles.
January 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

Marc-André Thibodeau January 16, 2019

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
Atlassian Team members are employees working across the company in a wide variety of roles.
January 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

Ricardo Herrera September 12, 2019

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?

Ricardo Herrera September 12, 2019

I found the answer here@prunge 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