Background
Sometimes many reports and subreports must use the same default format (e.g., "YYYY/MM/dd") while only a handful of fields are an exception (e.g., "YYYY/MM").
Question
What are the exact steps to ensure all date fields use the same date format, which can be changed in one location, without having to modify each field individually, such that individual fields can easily override the default?
Research
Here is a list of resources that fail to answer this question:
- http://community.jaspersoft.com/questions/543426/settign-default-format-... - no answer
- http://community.jaspersoft.com/questions/528989/decimal-formatting-roun... - suggests "sending an instance" of a DefaultFormatFactory subclass, but does not explain how
- http://stackoverflow.com/a/4018242/59087 - suggests setting a FormatFactory report property ("formatFactoryClass"), but does not explain how
- http://community.jaspersoft.com/wiki/how-show-pure-arabi%D1%81-numbers-r... - provides an example DefaultFormatFactory subclass, but no answer shows how to use it
- http://community.jaspersoft.com/questions/539398/dont-get-custom-formatf... - shows a line of code that sets the REPORT_FORMAT_FACTORY parameter, but doesn't show how to get the reportParams instance, and also doesn't use Jaspersoft Studio
- http://community.jaspersoft.com/documentation/tibco-jaspersoft-studio-us... - suggests that "the user can replace the default one" but doesn't mention how; also, refers to a "format factory class" property name, but doesn't indicate how to set it (from withint Jaspersoft Studio)
- http://www.dynamicreports.org/forum/viewtopic.php?f=1&t=250 - some code is given, but isn't a comprehensive example
- http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/JRP... - suggests using the "formatFactoryClass" attribute of the report template, but doesn't say how to set it (i.e., where to go within Jaspersoft Studio to change that particular attribute)
- http://www.spagoworld.org/jforum/posts/list/46.page - drops a "dateformat" in the REPORT_PARAMETERS_MAP, but doesn't indicate whether it is used globally, how it was set, etc.
- http://community.jaspersoft.com/questions/517926/date-field-timezone-loc... - shows how to instantiate a custom DateFormatter, but must be applied on a field-by-field basis
The Definitive Guide seems to copy the Javadocs verbatim, which doesn't offer any additional insight.
Ideas
Here's how I thought it might be possible to set the formatFactoryClass attribute for a given report:
- Create a new Project
- Open Project Explorer
- Right-click on project name
- Select Properties
- Expand Jaspersoft Studio
- Click Properties
- Click Configure Workspace Settings button
- Click Add
- Set property name to any of: {net.sf.jasperreports.formatFactoryClass, or formatFactoryClass, or format.factory.class}
- Set value to (supply your own instance): com.package.reports.ReportFormatFactory
- Click OK to close Properties Dialog
- Click OK to close Preferences
- Click OK to close Properties for Project
This would also require a Library that includes the com.package.reports.ReportFormatFactory class inside.
2 Answers:
- Click Window >> Prefereces
- Select Capabilities
- Check Development
- Click OK
Next:
- Open the Project Explorer
- Right-click Project name
- Select Properties
- Select Java Build Path
- Click the Source tab
- Click Add Folder
- Select Create New Folder
- Set Folder Name to: src
- Click Finish
- Select src
- Click OK
- Set Default output folder: Project Name/build
- Click OK
Create a report as usual (with a text field that uses a date, either a parameter or a field), then:
- Select the report in the Outline panel
- Open the Properties panel
- Set Format Factory Class to: com.company.reports.ReportFormatFactory
Next, create some source code inside the "src" directory in a package (folder) named "com.company.reports". Paste the following into a file named "ReportFormatFactory.java" that is saved in that directory:
When you run the report, the date should be formatted as YYYY/MM/dd.
If you want to change the format (e.g., to "dd/MM/YYYY"), update the date format line in the source code and then restart Jaspersoft Studio (the classloader does not seem to reload the ReportFormatFactory class after modification).
To avoid having to restart each time the date format changes, use a resource bundle:
- Create a new project folder called i18n
- Right-click on the project name
- Select New >> Folder
- Set Folder name to i18n
- Click Finish
- Right-click on i18n
- Select New >> Other
- Expand Messages Editor
- Select ResourceBundle
- Click Next
- Set the name to ReportsLocale
- Add a Locale (e.g., en_US)
- Click Finish
Add the i18n directory to the build process:
- Right-click i18n
- Select Build Path >> Configure Build Path
- Click Add Folder
- Check i18n
- Click OK
- Click OK again
Change the createDateFormat method:
And add these constants to the class definition (immediately after the publc class declaration, around line 15/16):
- Edit the ReportsLocale file
- Add a date.format property
- Set the property value to: dd/MM/YYYY
- Set the property value for all locales.
When the report is run, the date should look like 29/02/1976, for example.
Have you considered creating a custom function?
http://community.jaspersoft.com/wiki/jaspersoft-studio-expression-editor...