If you want to set a default date for a jasperreports parameter, you can enter the following in the default value expression of the parameter.
new Date()
However, if you wanted the default to be relative from today, for example, 3 days ago, you will need to do go outside of the standard java date libraries (Date, Calendar) because JR parameter does not allow you to have multiple statements. You can write your own utility package or you can use Joda, a replacement library for the JDK date and time library.
To use Joda in iReports, you need to extract the jar file and add it to Options -> Classpath. You may also drop the jar file in iReports' lib directory. You will also need to add this library to your application or report server.
For setting a default date 5 days prior from today, use this:
new org.joda.time.DateTime().minusDays(5).toDate()
or this if you want the time part set to the beginning of the day:
new org.joda.time.LocalDate().toDateTimeAtStartOfDay().minusDays(5).toDate()
or if you want the parameter to be relative to another parameter:
new org.joda.time.LocalDate($P{DateParam}).toDateTimeAtStartOfDay().minusDays(5).toDate()
Joda has other methods to get a relative date: minusWeeks, minusMonths, minusYears, plusDays, plusWeeks, plusMonths, plusYears. I think you can also use Groovy to write an expression to get a relative date but that's for another time. It would probably be as simple as new Date() - 5. Post a comment if you came up with a better way.
new Date()
However, if you wanted the default to be relative from today, for example, 3 days ago, you will need to do go outside of the standard java date libraries (Date, Calendar) because JR parameter does not allow you to have multiple statements. You can write your own utility package or you can use Joda, a replacement library for the JDK date and time library.
To use Joda in iReports, you need to extract the jar file and add it to Options -> Classpath. You may also drop the jar file in iReports' lib directory. You will also need to add this library to your application or report server.
For setting a default date 5 days prior from today, use this:
new org.joda.time.DateTime().minusDays(5).toDate()
or this if you want the time part set to the beginning of the day:
new org.joda.time.LocalDate().toDateTimeAtStartOfDay().minusDays(5).toDate()
or if you want the parameter to be relative to another parameter:
new org.joda.time.LocalDate($P{DateParam}).toDateTimeAtStartOfDay().minusDays(5).toDate()
Joda has other methods to get a relative date: minusWeeks, minusMonths, minusYears, plusDays, plusWeeks, plusMonths, plusYears. I think you can also use Groovy to write an expression to get a relative date but that's for another time. It would probably be as simple as new Date() - 5. Post a comment if you came up with a better way.
Comments
Do you know of a neat way to print a calendar in iReports? The goal it to print a calendar with various dates filed in based on data in tables.
Cheers,
Rich
new Date().plus(7) //7 days from today
Best regards.
I have absolutely no knowledge of coding however I had been hoping to start my own blog soon.
Anyhow, should you have any recommendations or techniques for new blog
owners please share. I know this is off topic nevertheless I simply needed to ask.
Appreciate it!
Take a look at my web site :: weight loss
my website > weight loss
I'd certainly donate to this outstanding blog! I guess for now i'll settle
for bookmarking and adding your RSS feed to my Google account.
I look forward to fresh updates and will talk about this site
with my Facebook group. Chat soon!
Feel free to visit my blog post http://gohima.com/