Jump to content
We've recently updated our Privacy Statement, available here ×

How to add/subtract days/hrs from date


soma

Recommended Posts

Hi,

I am a new user in iReport and have a requirement to add days to date value passed by the user. Then do a sql query based on the new date. Can someone tell me how can I do this? I am using iReport 1.3.3 and MySQL.

 

Regards

Soma

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

First of all, most of the java.util.Date class's methods have been deprecated by java.util.Calendar methods. So my first suggestion is to use java.util.Calendar.-But thats just a suggestion.

 

If I understand you correctly, you are receiving a 'Date' object from your parameter. Change this to java.util.Calendar.

 

I would create a variable:

 

Name = daysAdded

Class = java.util.Calendar()

Reset Type= NONE

Calculation Type = NONE

Increment Type = NONE

Initial Expression = $P{Name of your incoming parameter}

Variable Expression = $P{NameOfYourParam}.CalendarFunctionToSetDays()

 

I don't know the exact function you need for the underlined above, but here is the link that you need.

 

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#method_summary

 

Then create a Parameter, Call it newDate.

Parameter expression should be $V{daysAdded}

Use this parameter in your query...

 

Let me know if this works...

Link to comment
Share on other sites

Hi,

Thanks for the reply, I was able to add the date but when I move my $V{adddate} to pramenter $P{newDate}, I am getting null value inside newDate. I am using the Default Parameter Expression box to move the adddate to newDate.

 

Regards

Soma

Link to comment
Share on other sites

If you only need to use the new date as part of a filter, consider doing it all in the query. If you have two parameters, one the date entered and one the number of days, you can use the MySQL DATE_ADD function where you need the new date. Your query would contain something like

 

Code:
WHERE some_field < DATE_ADD($P{date}, INTERVAL ${num_days} DAY)

 

-Barry

Post edited by: bklawans, at: 2007/07/27 00:06

Link to comment
Share on other sites

I was able to add the date but when I move my $V{adddate} to pramenter $P{newDate}, I am getting null value inside newDate

 

This raises the question: Which value gets filled first, variable values or parameter values...?

 

How is it that you know the 'null' value exists? Are you displaying it in a textfield?

If so, try changing the evaluation time of your textfield, see if that will give you the value of $V{adddate} inside of $P{newDate}..

 

Does this work?

Link to comment
Share on other sites

  • 9 years 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...