Our new Appfire Documentation Space is now live!

Take a look here! If you have any questions please email support@appfire.com

Calculate the duration between a date field's current and previous value

Abstract

This code snippet fetches the duration between a date field's current and previous value.

Logic

  • Check for null values.
  • Fetch the field history of the date field.
  • Return the difference between the two dates in Long.

Snippet

import java.text.SimpleDateFormat;
 
Date curDate = issue.get("<Date field>");
if (curDate == null)
  return null;
 
if (issue.getFieldHistory("<Date field>").size() == 0)
    return null;
oldVal = issue.getFieldHistory("<Date field>")[-1].getFrom()
 
if (oldVal==null)
 return null;
 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date oldDate = sdf.parse(oldVal);
return (curDate.getTime() - oldeDate.getTime())/1000L;

Placeholders

PlaceholderDescriptionExample
<Date field>Date in contextDue date

Examples

The output of the code snippet is a duration which you could use in a Groovy script, for example, to calculate and display the duration between the current Due date and the last Due date.

import java.text.SimpleDateFormat;
 
Date curDate = issue.get("duedate");
if (curDate == null)
  return null;
 
if (issue.getFieldHistory("duedate").size() == 0)
    return null;
oldVal = issue.getFieldHistory("duedate")[-1].getFrom()
 
if (oldVal==null)
 return null;
 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date oldDate = sdf.parse(oldVal);
return (curDate.getTime() - oldDate.getTime())/1000L;

References