Jump to content

java.util.calendar help


quantzie

Recommended Posts

Hi everyone! I'm a beginner at coding and I've been trying out iReports. I would like to get an output of a date that will be 1 year before today each time I run the reports. I'm using PostgreSQL with iReport 3.7.4. I've the latest java version and I still can't find the class java.util.calendar inside iReports. The only ones that I can see are java.util.dates and java.sql.timestamp. How do I import in the java.util.calendar because I have searched around the internet and I can find examples of people using it. Thanks!

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

I think you can get what you need out of java.util.date.  Define a variable called $V{OneYearAgo} as type java.util.Date, and set the Evaluation expression to    

new Date()-365

(or, to be more precise, define $V{Today} as new Date() and then $V{OneYearAgo} as:

new Date($V{Today}.getAt(Calendar.YEAR)-1900-1,$V{Today}.getMonth(),$V{Today}.getDate())

Dates have all kinds of pitfalls...in my case, I'm using Groovy, and I show that the getAt(Calendar.YEAR) pulls a four digit year, but that the Date() constructor expects number of years since 1900.  And I show that getMonth results in a month from 0 to 11, not 1 through 12, but luckily it is actually the same interpretation of month that the Date() constructor uses at least!

Link to comment
Share on other sites

I almost forgot to mention: I think this will work fine when you run it interactively.  If you go to schedule it, I think I've read on this forum that the date (and everything else) will be interpreted at the time you schedule, not at the time it actually runs, so be aware of that limitation.

Link to comment
Share on other sites

I didn't think that Groovy was so powerful! 

I use Joda, a Java framework for dates.

 If i have to filter rows by a date I use a parameters as DATE_FROM.

The default value for DATE_FROM can be:

new org.joda.time.DateMidnight().minusDays(365).toDate()

or

new org.joda.time.DateMidnight().minusYears(1).toDate()

Joda is very useful to work with dates on a single line of code.

You can add Joda's jar to iReport or Tomcat/JasperServer classpath. 

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