Reputation: 1665
In iReport I want to set the default value of a java.util.Date type parameter to '8 September 2009'.
I tried setting this value in double quotes, with new java.util.Date(2009,9,8)
, with new java.util.Date(1252348200)
and with new java.util.Date("08-09-2009")
, but none of these work.
How can one set the default value for a date parameter?
Upvotes: 2
Views: 8960
Reputation: 3782
I can't find any way to directly do this, but here's a workaround:
1) First make the parameter's type String
rather than Date
.
2) Then enter a Default value expression
for the date literal like this: "2018-01-23"
3) Finally, use something like the following, which might need to get adjusted for the type of SQL you're using:
STR_TO_DATE($P{From},"%Y-%m-%d")
, where you need to first replace 'From' with the name of your actual parameter.This allows you to easily edit the default value as a string, and then use it as a date type in your SQL.
SQL usage example:
`WHERE `journal`.`Date` >= STR_TO_DATE($P{From},"%Y-%m-%d")`
Upvotes: 0
Reputation: 59
Pass a field/paramter, DATE_OBJ(java.util.Date) and a parameter, dateFormat(java.text.DateFormat) to the report. You can use a Calendar to set the required date on the DATE_OBJ Then the below expression should print your date.
Upvotes: 0
Reputation: 1976
You need to create new parameter set its type as java.util.Date and add in your text field's expression as
new SimpleDateFormat("dd MMMM, yyyy").format($P{dateParameter})
when you click preview a popup for input will appears.
MORE
To add new parameter:
click on Window -> Report Inspector (new palette will open on left pan) right click on parameter -> Add Parameter -> than set parameter class to 'java.util.Date' from its properties
Set as Expression:
Open expression editor of your field (right click -> Edit Expression) and paste
new SimpleDateFormat("dd MMMM, yyyy").format($P{dateParameter})
Upvotes: 2
Reputation: 346
According to Nitin Dandriyal's answer:
You can use this code in the Default Value Expression using GroovyShell as follow:
new groovy.lang.GroovyShell().evaluate("Calendar cal = Calendar.getInstance(); cal.set(2009, 8, 8); return cal.getTime(); ")
Update:
Upvotes: 0
Reputation: 148
The Date constructor with three parameters : year, month and day is deprecated. You should use instead the GregorianCalendar class. Howerver, if you really need a Date object, use the getTime(); method that returns a Date object from a GregorianCalendar instance.
GregorianCalendar gc = new GregorianCalendar(2009, Calendar.SEPTEMBER, 8);
Date myDate = gc.getTime();
Upvotes: -1
Reputation: 1607
Use calendar
Calendar c = Calendar.getInstance();
c.set(2009, 8, 8);
Date d = c.getTime();
Upvotes: 0