How to use relative dates in jrxml static reports?

Table of Contents 

Issue Description:

The following are a couple of examples on how to use relative dates in jrxml report.


Resolution:

You can define two parameters and two date type input controls with the following names, e.g. BeginDate and EndDate. The two input controls will be initialized with the respective parameter defaultvalueexpression.

<parameter class="net.sf.jasperreports.types.date.DateRange" name="BeginDate">
    <defaultvalueexpression>
        <![CDATA[new net.sf.jasperreports.types.date.DateRangeBuilder("WEEK-14").toDateRange()]]>
    </defaultvalueexpression>
</parameter>
<parameter class="net.sf.jasperreports.types.date.DateRange" name="EndDate">
    <defaultvalueexpression>
        <![CDATA[new net.sf.jasperreports.types.date.DateRangeBuilder("WEEK-1").toDateRange()]]>
    </defaultvalueexpression>
</parameter>

You can use these parameters in the query in conjunction with $X, e.g.

$X{BETWEEN, column, BeginDate, EndDate}

Another Example, retrieve results prior to the end of last month

<parameter class="net.sf.jasperreports.types.date.DateRange" name="EndDate">
    <defaultvalueexpression>
        <![CDATA[new net.sf.jasperreports.types.date.DateRangeBuilder("MONTH-1").toDateRange().getEnd()]]>
    </defaultvalueexpression>
</parameter>

Query requires $X syntax since $P function does not support relative date classes.

SELECT * FROM orders 
 WHERE $X{LESS, order_date, EndDate} 

Ref. Case #00034561

Comments

I'd like to be able to put into the web report (Month-1) for a scheduled report. When running this in the Studio editor this works fine but when published to the server, it errors stating "Specify a valid value for type Date". This feature appears to be broken.

Feedback