[英]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'. 在iReport中,我想将java.util.Date类型参数的默认值设置为'2009年9月8日'。
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. 我尝试用new java.util.Date(2009,9,8)
, new java.util.Date(1252348200)
和new java.util.Date("08-09-2009")
,但这些都不起作用。
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. 您应该改用GregorianCalendar类。 Howerver, if you really need a Date object, use the getTime() ; 但是,如果确实需要Date对象,请使用getTime() ; method that returns a Date object from a GregorianCalendar instance. 从GregorianCalendar实例返回Date对象的方法。
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 您需要创建一个新参数,将其类型设置为java.util.Date并在文本字段的表达式中添加为
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 单击窗口->报告检查器(新面板将在左窗格中打开),右键单击参数->添加参数->然后从其属性中将参数类设置为'java.util.Date'
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 : 根据Nitin Dandriyal的回答 :
You can use this code in the Default Value Expression using GroovyShell as follow: 您可以在使用GroovyShell的默认值表达式中使用此代码,如下所示:
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. 将字段/参数DATE_OBJ(java.util.Date)和参数dateFormat(java.text.DateFormat)传递给报表。 You can use a Calendar to set the required date on the DATE_OBJ Then the below expression should print your date. 您可以使用日历在DATE_OBJ上设置所需的日期,然后以下表达式将打印您的日期。
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
. 1)首先使参数的类型为String
而不是Date
。
2) Then enter a Default value expression
for the date literal like this: "2018-01-23"
2)然后为日期文字输入Default value expression
,如下所示: "2018-01-23"
3) Finally, use something like the following, which might need to get adjusted for the type of SQL you're using: 3)最后,使用类似以下的内容,可能需要针对所使用的SQL类型进行调整:
STR_TO_DATE($P{From},"%Y-%m-%d")
, where you need to first replace 'From' with the name of your actual parameter. 对于MariaDB(或MySQL)SQL,请使用: STR_TO_DATE($P{From},"%Y-%m-%d")
,在这里您需要首先用实际参数的名称替换'From'。 This allows you to easily edit the default value as a string, and then use it as a date type in your SQL. 这使您可以轻松地将默认值编辑为字符串,然后在SQL中将其用作日期类型。
SQL usage example: SQL用法示例:
`WHERE `journal`.`Date` >= STR_TO_DATE($P{From},"%Y-%m-%d")`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.