Jump to content
Changes to the Jaspersoft community edition download ×

Global Date Format pattern


raffimd

Recommended Posts

Hi,

In my product, we have common date format. I want to set the same global date format for all the date fields in all the jasper Reports.

 

I have observed that the JasperReports 1.3.2 have some new features to set the format factory. I tried it, but could not make it work.

 

Here is the details:

 

I have extended the DefaultFormatFactory and implemented my date format if the pattern is null. Basically I want to apply the global pattern if the specific pattern is not mentioned in JRXML.

Code:

public class XFormatFactory extends DefaultFormatFactory{

public java.text.DateFormat createDateFormat(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timezone)
{
java.text.DateFormat formattor = null;
if (pattern == null)
{
//In real code, I'll get the
//format from configuration
formattor = new SimpleDateFormat("yyyyMMdd_HHmmss_SSSZ"«»);
return formattor;
}
else
{
return super.createDateFormat(pattern, locale, timezone);
}
}
}

 

Then I have created an instance of the above Class and set as the formattor in the report parameters that I pass to the JasperFillManager.fillReport() API.

Code:
[code]XFormatFactory jasperFormatFactory = new XFormatFactory();
finalReportParameters.put(JRParameter.REPORT_FORMAT_FACTORY, jasperFormatFactory);

 

I debugged my code in Eclipse, the JasperReports is not calling my formattor class for the Date fields.

Do I miss anything?

Is there a way to set the global date format for all the date fields in the jasperReports?

 

Thanks in Advance,

Raffi

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi Lucian,

I really appreciate the quick response.

I have attached the testReport.jrxml, which has only one field that displays the current date [new Date()].

I just want that date to be in the global format that I set in the formattor. I do not want to specify the pattern for each field in the report, which is difficult to loaclize.

 

Thanks a lot,

Raffi [file name=testReport.jrxml size=2304]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/testReport.jrxml[/file]

Link to comment
Share on other sites

Still works fine for me (see the attached PDF).

 

Can you check again your application's classpath to make sure you don't have two JR jars on it, and your code to make sure you actually set the parameter? Otherwise, post a full sample (including Java code to run the report), because I have no clues on why it doesn't work for you.

 

Regards,

Lucian [file name=testReport_0_0.pdf size=1197]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/testReport_0_0.pdf[/file]

Link to comment
Share on other sites

Lucian,

Thanks for testing it. That might be the reason, I just downloaded 1.3.2 today. i'll double check the classpath, test it and post the results by tommorrow.

 

BTW, thanks to you guys for introducing that capabilities in JR1.3.2 :)

Raffi

Link to comment
Share on other sites

Lucian,

You are right, I had 2 jars in one place :(

After deleting that it worked :)

 

One small change I did in my code. The pattern is coming as empty string instead of null. So I have added the check for null and empty string.

It is working perfectly.

Code:

public class XFormatFactory extends DefaultFormatFactory{



public java.text.DateFormat createDateFormat(java.lang.String pattern, java.util.Locale locale, java.util.TimeZone timezone)

{

java.text.DateFormat formattor = null;

if (pattern == null)

{

//In real code, I'll get the

//format from configuration

formattor = new SimpleDateFormat("yyyyMMdd_HHmmss_SSSZ"«»);

return formattor;

}

else

{

return super.createDateFormat(pattern, locale, timezone);

}

}

}

 

Thanks a lot,

Raffi

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...