Jump to content

how to subtract months from a date?


dprogrammer

Recommended Posts

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

  • 2 weeks later...
  • 2 weeks later...

This is indeed a common requirement. As belmus alludes to, you can generally perform this calculation in the SQL query. The syntax can be ugly, and of course that doesn't work if you aren't using SQL. I like to do this calculation in the SQL query most times. But when that's not possible...

 

The best solution that I know of to do this calculation inside the report itself is to use Apache Commons Lang. Get the .jar file. Put in in the classpath (using Tools -> Options -> Classpath). Then use code like this:

 

org.apache.commons.lang.time.DateUtils.addMonths($P{MyDate},-1)

 

Nice, eh? (The big qualified classname makes it look worse than it is.)

 

Regards,
Matt

Link to comment
Share on other sites

CBox,

 

Yes, I was too lazy to mention the importing of the package. But that makes for much more readable code. I finally got around to posting a sample report that does some date calculations. (And I followed your suggestion of using the import!)

 

For anyone interested, my post includes a discussion of date functions in JR and a sample report with some useful date calculations like "MyDate - 3 days" and "the first day of the month".

 

Regards,
Matt

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