Jump to content

Dynamic date values for Parameter when schedule


anandharaj

Recommended Posts

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.

Link to comment
Share on other sites

  • Replies 12
  • Created
  • Last Reply

Top Posters In This Topic

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Thanks lucianc,

 

I found this code in Inet.

 

Code:
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE, -1);
java.util.Date date = cal.getTime();

 

How to use this code in JR. Meaning parse to a parameter?

 

Can anyone help me on this matter. Im stack on this matter.

 

Urgent...

 

I'll appreciate if you guys can give with example code (jrxml)

Post edited by: anandharaj, at: 2007/01/09 05:34

Link to comment
Share on other sites

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:

package my.package;

public class MyDateUtils {
public static Date yesterday() {
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
}

-----

<parameter name="yesterday" class="java.util.Date" isForPrompting="false">
<defaultValueExpression>my.package.MyDateUtils.yesterday()</defaultValueExpression>
</parameter>

 

Or you can use Commons Lang and do (you would obviously need the Commons Lang jar on your classpath)

Code:
[code]
<parameter name="yesterday" class="java.util.Date" isForPrompting="false">
<defaultValueExpression>org.apache.commons.lang.time.DateUtils.addDays(new Date(), -1)</defaultValueExpression>
</parameter>

 

HTH,

Lucian

Link to comment
Share on other sites

  • 2 years later...

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.

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

  • 2 months later...

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:
 (CASEWHEN $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)
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...