Date function compile problem

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 

 
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. The method DATEFORMAT(Date, String) is undefined for the type Product_1428678178249_632342
                value = DATEFORMAT(((java.util.Date)field_productDate.getValue()),"EEEE dd MMMM yyyy"); //$JR_EXPR_ID=10$
                        <-------->
2. The method DATEFORMAT(Date, String) is undefined for the type Product_1428678178249_632342
                value = DATEFORMAT(((java.util.Date)field_productDate.getOldValue()),"EEEE dd MMMM yyyy"); //$JR_EXPR_ID=10$
                        <-------->
3. The method DATEFORMAT(Date, String) is undefined for the type Product_1428678178249_632342
                value = DATEFORMAT(((java.util.Date)field_productDate.getValue()),"EEEE dd MMMM yyyy"); //$JR_EXPR_ID=10$
                        <-------->
3 errors

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 ?

 

<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.0.0</version>
<exclusions>
<exclusion>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
benkdishman's picture
Joined: Apr 5 2015 - 7:25am
Last seen: 7 years 7 months ago

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

tchen's picture
121140
Joined: Feb 27 2008 - 7:33am
Last seen: 2 days 19 hours ago

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.

benkdishman - 7 years 11 months ago

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.

tchen - 7 years 11 months ago

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.

sabmoonie - 7 years 8 months ago
Feedback
randomness