I have a created a scripted field which calculates the amount of hours between two dates that are recorded in two other custom fields. Now I am being asked to display the time in " XX D xx H xx M but I can not figure out how to do that. I have also been asked if it is possible to have this calculation exclude weekends. Is this at all possible? Please help!
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import java.util.Date.*
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_15968');
def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_15967');
if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {
def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2 = issue.getCustomFieldValue(dateFieldObject2) as Date
def calculation = (dateValue.getTime() - dateValue2.getTime()) / 3600000
return (calculation as String)
}
Hi Christopher,
This is a tricky one, however I think it could be done by doing something like this:
import java.util.concurrent.TimeUnit
def date1 = new Date()
def date2 = new Date() - 2
def time = date1.getTime() - date2.getTime()
def form = String.format("%d hours, %d min, %d sec",
TimeUnit.MILLISECONDS.toHours(time),
TimeUnit.MILLISECONDS.toMinutes(time),
TimeUnit.MILLISECONDS.toSeconds(time)
);
return form
No entirely sure how you would go about excluding weekends. By that do you mean lets say you 04/01/2018: 00:00:02 which is a Monday and a 01/01/2018: 00:00:01 which is a Friday you exclude the weekend so the time between them is 0 days, 0 hours, 1 min instead of 2 days, 0 hours, 1 min?
In that case you'd have to find out how many weekend days occurred between the 2 dates, then times 86,400,000 (Milliseconds in a day) by the amount of days and minus that off your total 'time' variable. something like that anyway, might not be completely correct.
Btw the scripted field needs to be a text field.
Thanks
Johnson Howard (Adaptavist)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.