1

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!

My problem MyReport

EDIT After answer, I would like to the show days to:

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Ashton
  • 363
  • 1
  • 4
  • 21

1 Answers1

0

You do not need variables for this, use directly the textFieldExpression

If the fields MO_DATECREATED and MO_DATECOMPLETED are declared:

as java.lang.Date

<field name="MO_DATECREATED" class="java.lang.Date">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>

The textFieldExpression would be ($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime())/(1000*60*60) + " hours".

Hey thats exactly so to understand what it does check out this: How to calculate time difference in java?

as java.lang.String

<field name="MO_DATECREATED" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>

We need to parse them to Date object's first.. your pattern is mm/dd/yy hh:mm a

(new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECOMPLETED}).getTime()-new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECREATED}).getTime())/(1000*60*60) + " hours"

Considering that they maybe null we better add a printWhenExpression as well

Complete result

<textField>
     <reportElement x="0" y="0" width="100" height="20" uuid="eac93a84-7901-4205-b09c-556d48dc05e1">
        <printWhenExpression><![CDATA[new java.lang.Boolean($F{MO_DATECREATED}!=null && $F{MO_DATECOMPLETED}!=null)]]></printWhenExpression>
    </reportElement>
    <textFieldExpression><![CDATA[(new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECOMPLETED}).getTime()-new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECREATED}).getTime())/(1000*60*60) + " hours"]]></textFieldExpression>
</textField>

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 a java.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 answered

This was the temporary solution

<textField>
     <reportElement x="0" y="0" width="100" height="20" uuid="eac93a84-7901-4205-b09c-556d48dc05e1">
        <printWhenExpression><![CDATA[new java.lang.Boolean($F{MO_DATECREATED}!=null && $F{MO_DATECOMPLETED}!=null)]]></printWhenExpression>
    </reportElement>
    <textFieldExpression><![CDATA[($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (24* 60 * 60 * 1000)  + " days " +($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 60 * 1000) % 24 + " hours " +  ($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 1000) % 60 + " minutes"]]></textFieldExpression>
</textField>
Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • How do i get this to show me the hours and minutes as well? – Ashton Jan 07 '16 at 18:07
  • It is in java.lang.Date formate and your java statements works! But it only shows hours- what's the math to display the hours and minutes? – Ashton Jan 07 '16 at 18:16
  • I am still having an issue of having it display the hours and minutes- thank you Petter! – Ashton Jan 07 '16 at 19:33
  • Ok lets try it... : ) are you using java8? – Petter Friberg Jan 07 '16 at 20:19
  • Yes and I am using iReport to write my reports :/ – Ashton Jan 07 '16 at 20:46
  • I posted a question on the java section http://stackoverflow.com/questions/34665069/how-to-create-a-single-expression-displaying-time-difference-between-two-dates, If no good answer comes in 10 minutes I will pass you some code to just display HH:mm – Petter Friberg Jan 07 '16 at 21:07
  • Ok I edit the answer to give you temporary answer on how to display hours and minutes... keep track of the question above... and maybe you will get a general answer on how to display these time differences... – Petter Friberg Jan 07 '16 at 21:31
  • Thank you for the help! But before it was pulling all the hours but now is not coparing the days as hours :/ – Ashton Jan 12 '16 at 15:18
  • I dont need days exactly but it'd be nice. The first code you posted gave me all of the hours like "123 hours" but now it only shows the difference in the time- not the date. So yes is there a way to get the difference in the days as well? It doesn't seem to be pulling the date information – Ashton Jan 12 '16 at 15:35
  • I am going to post a picture – Ashton Jan 12 '16 at 15:36
  • Where is it? I dont see it :3 – Ashton Jan 12 '16 at 15:39
  • Petter- is there a way I can calculate the average of all of the times posted in iReport? – Ashton Jan 15 '16 at 19:26
  • Yes create a variable set expression of what you like to calc and use type avgerage – Petter Friberg Jan 16 '16 at 12:26
  • Petter the type average does not seem to work with your hard coded equation to find the days, hour and minutes. Any suggestions? – Ashton Jan 19 '16 at 17:55
  • I did and it only displays the very last value in the Time column – Ashton Jan 19 '16 at 19:29
  • okay and could you maybe look at the new question i posted as well? I appreciate all of the help you have done for me! – Ashton Jan 19 '16 at 19:40
  • @Ashton see this [answer](http://stackoverflow.com/a/37579395/5292302) by Tunaki it very nice. – Petter Friberg Jun 01 '16 at 22:15