Jump to content
Changes to the Jaspersoft community edition download ×

Date input: previous month, previous week, date to date.


CaPsLoCkEr

Recommended Posts

Hello

 

I am currently creating a report in jaspersoft studio which will eventually be published on the jaspersoft server. For the report I would like it to prompt for a date. The option I want to include are previous week, previous month and just date tot date. 

 

The date to date part is already workin. I made 2 parameters (start_date and end_date) and made them work in my query. 

The query is looking something like this:

SELECT
count(*) 'Total Alarms',
sum(case when condition_id like '1' then 1 else 0 end )as 'Total Minor Alarms',
sum(case when condition_id like '2' then 1 else 0 end )as 'Total Major Alarms',
sum(case when condition_id like '3' then 1 else 0 end )as 'Total Critical Alarms'
from alarminfo
where
 alarminfo.set_time>=$P{start_date} AND alarminfo.set_time<$P{end_date}

 

You can see in the last line my 2 parameters. Now I also want the report to prompt a previous week and previous month option. I already searched and read quite a lot artikels and topics, but i still cannot implement it to what I need. Does anyone have an idea? 

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Jasper has tons of built in support for dynamic date ranges, this is a common problem with a common solution. In fact, Jasper Reports has a specific "DateRange" type that can do a lot of the heavy lifting for you. Handles things like "Date +1 week" etc.

 

https://community.jaspersoft.com/wiki/how-use-relative-dates-tibco-jaspersoft-studio-report

Link to comment
Share on other sites

I have read a few of articles on the community but I fail to implement them. 

 

For now I have 2 variables: firstdayoflastmonth and lastdayoflastmonth

for firstdayoflastmonth I  am using this code:

(
    $P{cal}.set($P{runDate}.getYear()+1900, $P{runDate}.getMonth(), $P{runDate}.getDate()) ||
    $P{cal}.add(Calendar.MONTH, -1) ||
    $P{cal}.set(Calendar.DAY_OF_MONTH, 1)
)
? null : $P{cal}.getTime()

 

I aslo created 2 parameters: runDate with value expression new Date()

and cal with value expression Calendar.getInstance()

When I run the report, I get this error: 

 

net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: (
    $P{cal}.set($P{runDate}.getYear()+1900, $P{runDate}.getMonth(), $P{runDate}.getDate()) ||
    $P{cal}.add(Calendar.MONTH, -1) ||
    $P{cal}.set(Calendar.DAY_OF_MONTH, 1)
)
? null : $P{cal}.getTime()
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:548)
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:523)
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler$5.run(ReportControler.java:404)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: (
    $P{cal}.set($P{runDate}.getYear()+1900, $P{runDate}.getMonth(), $P{runDate}.getDate()) ||
    $P{cal}.add(Calendar.MONTH, -1) ||
    $P{cal}.set(Calendar.DAY_OF_MONTH, 1)
)
? null : $P{cal}.getTime()
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:290)
    at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:616)
    at net.sf.jasperreports.engine.fill.JRCalculator.initializeVariable(JRCalculator.java:490)
    at net.sf.jasperreports.engine.fill.JRCalculator.initializeVariables(JRCalculator.java:313)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:237)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582)
    at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFill.run(BaseFillHandle.java:123)
    at java.lang.Thread.run(Thread.java:745)
Caused by: groovy.lang.MissingMethodException: No signature of method: java.lang.String.getYear() is applicable for argument types: () values: []
Possible solutions: getChars(), getChars(int, int, [C, int), getAt(groovy.lang.EmptyRange), getAt(groovy.lang.IntRange), getAt(java.lang.String), getAt(int)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at Engie_Total_Alarms_1511272037717_2640.evaluate(calculator_Engie_Total_Alarms_1511272037717_2640:191)
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:276)
    ... 8 more
 

Link to comment
Share on other sites

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...