I am currently writing a report to calculate the TotalTimeDifference
between two date variables in iReport.
The two variables that I am comparing are MO_DATECREATED
and MO_DATECOMPLETED
and I am trying to calculate the time difference only.
I have tried setting up a variable that does a simple subtraction of the two variables- and of course that has not worked at all.
I will attach photoes of what I currently have but I am looking for the way to compare the two variables (which contains date/time) and printing out a variables with the difference in time.
Example: If the MO was started at 1/2/15 12:55pm
and completed at 1/3/15 1:55pm
i want to print the time difference, or how long it took, as 25 hours
How can I do this in iReport? Thank you for helping out a newbie!
EDIT After answer, I would like to the show days to:
You do not need variables for this, use directly the
textFieldExpression
If the fields
MO_DATECREATED
andMO_DATECOMPLETED
are declared:as
java.lang.Date
The
textFieldExpression
would be($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime())/(1000*60*60) + " hours"
.Hey thats java exactly so to understand what it does check out this: How to calculate time difference in java?
as
java.lang.String
We need to parse them to
Date
object's first.. your pattern ismm/dd/yy hh:mm a
Considering that they maybe
null
we better add aprintWhenExpression
as wellComplete result
No doubt that it is better that they are
java.lang.Date
object, both the report will fill faster, no risk for parsing error and you can export correctly to excel ecc. To format ajava.lang.Date
object as you wish just use the pattern property.EDIT: Users has opted for the
java.util.Date
and asked how he can display also minutes, for this I have created a general question on How to create a single expression displaying time difference between two Date's as years, months, days, hours, minutes, seconds and it is now answeredThis was the temporary solution