简体   繁体   中英

How to default value of java.util.Date type parameter in ireport?

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?

Use calendar

    Calendar c = Calendar.getInstance();
c.set(2009, 8, 8);
Date d = c.getTime();

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();

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})

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: 在此处输入图片说明

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.

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:

  • For MariaDB (or MySQL) SQL use: 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")`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM