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?
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.