Jump to content
We've recently updated our Privacy Statement, available here ×
  • Relative Dates are slightly incorrect in terms of exact time expected


    ghudson_1
    • Features: Ad Hoc, Filters, Input Controls, JasperReports Server, Parameters Version: v5.6 Product: JasperReports® Server

    Issue Description

    In certain circumstances Relative Dates seem to have incorrect values. For example in an Ad hoc from topic, relative date input control values seem flip-flipped or reversed.

    Resolution

    The DateRange class uses a few methods to determine the exact timestamp it should use, these are getStart() and getEnd() . So "DAY" theoretically could be the beginning of the day, or the end of the day when using day ranges. There is a bug open for investigation in which it seems the $X syntax uses the wrong order for calling getStart and getEnd on the begin and end inputs.

    For example using "DAY" for both START_DATETIME and END_DATETIME in a $X{Between yields:

    select * from employee where ( hire_date > ? AND hire_date < ? )
    

    Parameter #1 (of type java.util.Date): Wed Jul 09 23:59:59 EDT 2014

    Parameter #2 (of type java.util.Date): Wed Jul 09 00:00:00 EDT 2014

    Using "WEEK" for both yields:

    select * from employee where ( hire_date > ? AND hire_date < ? )
    

    Parameter #1 (of type java.util.Date): Sat Jul 12 23:59:59 EDT 2014

    Parameter #2 (of type java.util.Date): Sun Jul 06 00:00:00 EDT 2014

    To help understand your case, In Manage -> Server Settings -> Log Settings, you can toggle the input control logging to DEBUG to see what times are getting resolved.

    You can workaround this issue within your JRXML by casting/forcing the Input Control that the user gives into a specific range. For example take:

    <parameter name="USER_START_DATETIME" class="net.sf.jasperreports.types.date.DateRange"/>
    

    Then derive the begin_date like: 

    <parameter name="begin_date" class="java.util.Date" nestedType="java.util.Date">
        <defaultValueExpression>
            <![CDATA[$P{USER_START_DATETIME}.getStart()]]>
        </defaultValueExpression>
    </parameter>
    

    With the above technique you'll always get the beginning most value... so the start of the week when WEEK is chosen, or the start of the day when DAY is chosen, etc. Depending on your needs, maybe you want to do this for the End as well.

    Ref. Case 00055476

    getstart_rel.jrxml


    User Feedback

    Recommended Comments

        <parameter name="USER_START_DATETIME" class="net.sf.jasperreports.types.date.TimestampRange"/>    <parameter name="begin_date" class="java.sql.Timestamp" nestedType="java.sql.Timestamp" isForPrompting="false">        <defaultValueExpression><![CDATA[$P{USER_START_DATETIME}.getStart()]]></defaultValueExpression>    </parameter>    <parameter name="USER_END_DATETIME" class="net.sf.jasperreports.types.date.TimestampRange"/>    <parameter name="end_date" class="java.sql.Timestamp" nestedType="java.sql.Timestamp" isForPrompting="false">        <defaultValueExpression><![CDATA[$P{USER_END_DATETIME}.getEnd()]]></defaultValueExpression>    </parameter>[/code]

    Not sure if things changed from this post to the new version (running 7.1), but I was having issues using this when trying to get relative date ranges like "Last Week", "Last Month". The reason was that I am working with Timestamp data, which I read on Jaspersoft's docs you can use TimestampRange. Using the examples from their docs still don't give me what I needed, so I used what you posted and converted it to timestamp, now it works great! The only weird thing that I can't seem to get to work is for the manual date, time selection to work through Studio, but it works fine in Web. So this works for both manually entered dates and for relative dates where it will get the Start, End of the day based on the input. Example: Last Month: MONTH-1, MONTH-1 will give me all activity for the first day of the month 00:00:00 and the last day of the month 23:59:59. Thank you so much for posting this, I don't think I could have gotten this far without it.

     

    Link to comment
    Share on other sites

    My previous commented solution doesn't work when the timestamp = 00:00:00. When I tested DAY-1 in both start, end it excludes this timestamp. :( It seems like the $X{Between should do >= AND < so that it captures what happens at 00:00:00. Wonder if there is a way to do change this for relative dates.

    Edit: There is a way to solve this by using:

    $X{[bETWEEN, <column_name>, <left_parameter_name>, <right_parameter_name>}[/code]

    The key difference is where the "[" value is used. You can find more information on the different built-in SQL clause functions here: http://jasperreports.sourceforge.net/sample.reference/query/

    Link to comment
    Share on other sites



    Guest
    This is now closed for further comments

×
×
  • Create New...