Search code examples
javajasper-reports

IReport Java Long subtraction incorrect (possibly overflow)


Trying to make a Jasper Report and I'm having troubles using Java's Date class. I am working with a .CSV file so unfortunately can't use SQL to solve my issues.

I've narrowed it down to this statement:

new Long( 1L * $V{Date2Long} -  1L * $V{Date1Long})

Where Date2Long and Date1Long are: (both variable classes are Date)

new Long($V{Date2}.getTime())
new Long($V{Date1}.getTime())

Where Date2 and Date1 are: (both variable classes are Long)

new SimpleDateFormat("MM/dd/yyyy HH:mm").parse($F{Date2CSVColumnName})
new SimpleDateFormat("MM/dd/yyyy HH:mm").parse($F{Date1CSVColumnName})

Using the date examples:
Date2 = "05/05/2015 13:22"
Date1 = "04/28/2015 16:54"

Difference should be = 6 days, 20 hours, 28 minutes

Using java's .getTime() I get:
Date2 = "1430850120000"
Date1 = "1430258040000"

ProductionDateInMilliseconds - CreateDateInMilliseconds:
My java equation above = 58860000 (0.68125 Days) <---- ERROR
Difference should be = 592080000 (6.85 Days)

The only assumption I can make is that I made an overflow error somewhere. But I don't see how that is possible when all I used was longs. Are longs just broken in IReport?


Solution

  • I have tried this and nothing wrong with the iReport. Here is my JRXML file-

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="322b2351-0f86-4ebd-9c51-905d97f5d8f8">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <parameter name="Date2CSVColumnName" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["05/05/2015 13:22"]]></defaultValueExpression>
        </parameter>
        <parameter name="Date1CSVColumnName" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["04/28/2015 16:54"]]></defaultValueExpression>
        </parameter>
        <variable name="Date2" class="java.util.Date">
            <initialValueExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy HH:mm").parse($P{Date2CSVColumnName})]]></initialValueExpression>
        </variable>
        <variable name="Date1" class="java.util.Date">
            <initialValueExpression><![CDATA[new SimpleDateFormat("MM/dd/yyyy HH:mm").parse($P{Date1CSVColumnName})]]></initialValueExpression>
        </variable>
        <variable name="Date2Long" class="java.lang.Long">
            <variableExpression><![CDATA[]]></variableExpression>
            <initialValueExpression><![CDATA[new Long($V{Date2}.getTime())]]></initialValueExpression>
        </variable>
        <variable name="Date1Long" class="java.lang.Long">
            <variableExpression><![CDATA[]]></variableExpression>
            <initialValueExpression><![CDATA[new Long($V{Date1}.getTime())]]></initialValueExpression>
        </variable>
        <title>
            <band height="138" splitType="Stretch">
                <textField>
                    <reportElement x="101" y="0" width="358" height="22" uuid="16e461b2-4c7a-49c6-802e-9bd66b818ce1"/>
                    <textFieldExpression><![CDATA[$V{Date2}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="101" y="22" width="358" height="22" uuid="5609ffc9-354c-48ba-bbda-4ef8753875a7"/>
                    <textFieldExpression><![CDATA[$V{Date1}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="101" y="58" width="358" height="22" uuid="70582d8a-2c68-463a-ad73-6068bd2c58fd"/>
                    <textFieldExpression><![CDATA[$V{Date2Long}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="101" y="80" width="358" height="22" uuid="e48af180-0bb1-4d19-b908-fa7b1a962f45"/>
                    <textFieldExpression><![CDATA[$V{Date1Long}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="101" y="116" width="358" height="22" uuid="71ffab1a-f70d-4601-978c-96b73ae0f268"/>
                    <textFieldExpression><![CDATA[new Long( 1L * $V{Date2Long} -  1L * $V{Date1Long})]]></textFieldExpression>
                </textField>
            </band>
        </title>
    </jasperReport>
    

    Have a look.