I have a report (javabean collection datasource) which uses a date function - DATEFORMAT($F{meetingDate},"EEEE dd MMMM yyyy"). The report runs fine in eclipse and if I remove the function it runs properly through java.
With the function I get the following errors though
After searching I have tried
1) dwonloading and adding jasperreports-functions-6.0.0.jar to the build pah
2) alterring the POM to include commons-lang-2.6.jar and excluding it from the Jasper dependency (see below).
this hasn't made any difference. Can anyone help ?
1 Answer:
DateFormat is an abstract Java class. You need to use one of the static factory methods through getDateInstance() instantiation to format a date for the current Locale, such as
DateFormat.getDateInstance().format($F{productDate})
To format your productDate field from SQL query resultant data set into "EEEE dd MMMM yyyy" pattern in the report display, you will need to first create a SimpleDateFormat object using your display pattern (new java.text.SimpleDateFormat("EEEE dd MMMM yyyy") then use this SimpleDateFormat object to format your date field .format($F{productDate}).
But ....... Why? JasperReports already has the build Date format function to do this for you. You can simply display your productDate field in the report with "EEEE dd MMMM yyyy" pattern:
<textField pattern="EEEE dd MMMM yyyy"> ...... <textFieldExpression><![CDATA[$F{productDate}]]></textFieldExpression> </textField>
Please give a try and this should work for all SQL date/time/timestamp types (and save you a lot of trouble by coding the date formatter yourself).
Not quite sure I understand where I would put the java code for the formatting (in the expression editor, or would I have to do that bit in Java code and pass it through ?). I guess this is a moot point though as your simple solution works fine (apologies I hadn't noticed this section in the editor, I'm still very new to Jasper). Thanks again.
JasperReports is a Java application and your report design will be converted and compiled as a Java executable to run the report. Therefore, expressions in the report design will accept valid Java class method as long as it produces a non void object.
Im trying to get the difference in years between two datetimes and same as benkdishman I encounter similar error. I dont quite understand what you're trying to say tchen. Im a newbie here and not being able to use functions that are supposed to be built in is frustrating.