Dynamic date values for Parameter when schedule

0
Hi..

I created a report which have an input value (date) and extract data based on that value. When i schedule the job, i need to specify the input value (parameter). How do i set so that the value is current date or current date - n days.

This is for report which run on daily basis.

Please help on this matter.
anandharaj's picture
Joined: Oct 18 2006 - 5:02pm
Last seen: 6 years 3 days ago

12 Answers:

0
If you define a report parameter named _ScheduledTime of type java.util.Date, the report scheduler will set as parameter value the job scheduled execution date/time. You can then create another parameter, set its default value as $P{_ScheduledTime} - n days and use this parameter in your query.

HTH,
Lucian
lucianc's picture
7091
Joined: Jul 17 2006 - 1:10am
Last seen: 2 weeks 1 day ago
0
Thanks for reply

Can you give further explanation on how to do this (the code). The $P{_ScheduledTime} is predefine JI parameter?
anandharaj's picture
Joined: Oct 18 2006 - 5:02pm
Last seen: 6 years 3 days ago
0
Are you meaning something like this?

<parameter name="_ScheduledTime" isForPrompting="false" class="java.util.Date"/>
<parameter name="myDate" isForPrompting="true" class="java.util.Date">
<defaultValueExpression ><![CDATA[$P{_ScheduledTime} - ]></defaultValueExpression>
</parameter>

Post edited by: anandharaj, at: 2007/01/04 14:53
anandharaj's picture
Joined: Oct 18 2006 - 5:02pm
Last seen: 6 years 3 days ago
0
JR report expressions are Java expressions, unless you use a non-Java based report compiler, such as Groovy. In Java, you cannot do date - 1 to subtract a number of days from a date.

If you want to do something like that, you would need to take a look at [url=http://java.sun.com/javase/6/docs/api/java/util/Calendar.html]java.util.... or use an utility class such as [url=http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/la...(java.util.Date,%20int)]DateUtils from Commons Lang.

HTH,
Lucian
lucianc's picture
7091
Joined: Jul 17 2006 - 1:10am
Last seen: 2 weeks 1 day ago
0
Thanks lucianc,

I found this code in Inet.

Code:
Calendar cal = new GregorianCalendar();<br />
cal.add(Calendar.DATE, -1);<br />
java.util.Date date = cal.getTime();</td></tr></tbody></table><br />
<br />
How to use this code in JR. Meaning parse to a parameter?<br />
<br />
Can anyone help me on this matter. Im stack on this matter.<br />
<br />
Urgent...<br />
<br />
I'll appreciate if you guys can give with example code (jrxml)<br>Post edited by: anandharaj, at: 2007/01/09 05:34
anandharaj's picture
Joined: Oct 18 2006 - 5:02pm
Last seen: 6 years 3 days ago
0
You can't put this code directly in the JRXML, you need to put it for instance in a static method of a class, then make sure that the compiled class is included in your application's classpath and call the method in the JRXML:
Code:
<br />
package my.package;<br />
<br />
public class MyDateUtils {<br />
  public static Date yesterday() {<br />
    Calendar cal = new GregorianCalendar();<br />
    cal.add(Calendar.DATE, -1);<br />
    return cal.getTime();<br />
  }<br />
}<br />
<br />
-----<br />
<br />
<parameter name="yesterday" class="java.util.Date" isForPrompting="false"><br />
  <defaultValueExpression>my.package.MyDateUtils.yesterday()</defaultValueExpression><br />
</parameter><br />
</td></tr></tbody></table><br />
<br />
Or you can use Commons Lang and do (you would obviously need the Commons Lang jar on your classpath)<br />
<table align="center" border="0" cellpadding="3" cellspacing="1" width="90%"><tbody><tr><td><b>Code:</b></td></tr><tr><td><pre><br />
<parameter name="yesterday" class="java.util.Date" isForPrompting="false"><br />
  <defaultValueExpression>org.apache.commons.lang.time.DateUtils.addDays(new Date(), -1)</defaultValueExpression><br />
</parameter><br />
</td></tr></tbody></table><br />
<br />
HTH,<br />
Lucian
lucianc's picture
7091
Joined: Jul 17 2006 - 1:10am
Last seen: 2 weeks 1 day ago
0
Thank you very much lucianc. I'll try it.
anandharaj's picture
Joined: Oct 18 2006 - 5:02pm
Last seen: 6 years 3 days ago
0
Hi Lucianc

I try the sample that you gave me...Its works fine.

Thank you very much.
anandharaj's picture
Joined: Oct 18 2006 - 5:02pm
Last seen: 6 years 3 days ago
0

After searching on how to set a daily scheduled task on a report whose date field has to default to yesterday (current day - 1), I came upon this thread, which seemingly would fix my problem, but alas, did not.

When I manually run the report, I can default the End Date to the previous day, and the report runs fine, giving me the previous day's information.

When I scheduled this report to run each day, I would expect the End Date to increment a day at a time, but instead, it saves the initial date that the schedule was made, and the result is that I get the same report every day.  End Date is always 07/04/2009.  I've played around with _ScheduledTime but have been unsuccessful.

Any help would be appreciated on how to make the date parameters dynamic within scheduled tasks.

Thanks.

jrechter's picture
Joined: Dec 17 2008 - 10:15am
Last seen: 3 years 11 months ago
0

Try something like this for the parameter.  The code below sets the date to the current date + 1 year

Code:
	<parameter name="vend_order_date_1_2" class="java.util.Date" isForPrompting="false">
		<defaultValueExpression><![CDATA[new java.util.GregorianCalendar(new java.util.GregorianCalendar().get(Calendar.YEAR) + 1,new java.util.GregorianCalendar().get(Calendar.MONTH),new java.util.GregorianCalendar().get(Calendar.DAY_OF_MONTH)).getTime()]]></defaultValueExpression>
	</parameter></td></tr></tbody></table>
carinmeier's picture
Joined: May 1 2009 - 5:52am
Last seen: 10 years 5 months ago
0

jrechter
Wrote:

After searching on how to set a daily scheduled task on a report whose date field has to default to yesterday (current day - 1), I came upon this thread, which seemingly would fix my problem, but alas, did not.

When I manually run the report, I can default the End Date to the previous day, and the report runs fine, giving me the previous day's information.

When I scheduled this report to run each day, I would expect the End Date to increment a day at a time, but instead, it saves the initial date that the schedule was made, and the result is that I get the same report every day.  End Date is always 07/04/2009.  I've played around with _ScheduledTime but have been unsuccessful.

Any help would be appreciated on how to make the date parameters dynamic within scheduled tasks.

Thanks.

Hi,

We solved this by adding some SQL to the report query to give us different scheduling options, and then you simply pass the parameter to the report for it to run (i.e. "Previous Day" or "Month to Date"). Below is the SQL as an example.

-Scott

Code:
 (CASE
WHEN $P{report_schedule} = "Month to Date" THEN (DATE(NOW()) + INTERVAL 1 DAY)
WHEN $P{report_schedule} = "Previous Month" THEN (DATE_FORMAT(CONCAT(YEAR(NOW()),"-",MONTH(NOW())-1,"-",DAY(LAST_DAY(NOW() - INTERVAL 1 MONTH))),"%Y-%m-%d") + INTERVAL 1 DAY)
WHEN $P{report_schedule} = "Previous Year" THEN (DATE_FORMAT(CONCAT(YEAR(NOW()),"-",MONTH(NOW())-1,"-",DAY(LAST_DAY(NOW() - INTERVAL 1 MONTH))),"%Y-%m-%d") + INTERVAL 1 DAY)
ELSE ($P{end_date_text} + INTERVAL 1 DAY)
END)</td></tr></tbody></table>
scottn's picture
6
Joined: Jan 9 2009 - 12:31pm
Last seen: 10 years 9 months ago
0

"_ScheduledTime" doesn't work for me!

I have:

1. defined a new parameter

<parameter name="_ScheduledTime" class="java.util.Date"/>

2. created a new Input Control named "_ScheduledTime"

3. scheduled new job for the report

I try to print $P{_ScheduledTime} on the report, but it is "null".

Why?

federico.cattozzi's picture
Joined: Sep 22 2009 - 4:34am
Last seen: 5 years 6 months ago
Feedback
randomness