Jump to content
Changes to the Jaspersoft community edition download ×

Canonical step-by-step guide to global date format in Jaspersoft Studio 6.x


thangalin
Go to solution Solved by thangalin,

Recommended Posts

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:

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:

  1. Create a new Project
  2. Open Project Explorer
  3. Right-click on project name
  4. Select Properties
  5. Expand Jaspersoft Studio
  6. Click Properties
  7. Click Configure Workspace Settings button
  8. Click Add
  9. Set property name to any of: {net.sf.jasperreports.formatFactoryClass, or formatFactoryClass, or format.factory.class}
  10. Set value to (supply your own instance): com.package.reports.ReportFormatFactory
  11. Click OK to close Properties Dialog
  12. Click OK to close Preferences
  13. Click OK to close Properties for Project

This would also require a Library that includes the com.package.reports.ReportFormatFactory class inside.

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • Solution
The following seems to work:
 
  1. Click Window >> Prefereces
  2. Select Capabilities
  3. Check Development
  4. Click OK

Next:

  1. Open the Project Explorer
  2. Right-click Project name
  3. Select Properties
  4. Select Java Build Path
  5. Click the Source tab
  6. Click Add Folder
  7. Select Create New Folder
  8. Set Folder Name to: src
  9. Click Finish
  10. Select src
  11. Click OK
  12. Set Default output folder: Project Name/build
  13. Click OK

Create a report as usual (with a text field that uses a date, either a parameter or a field), then:

  1. Select the report in the Outline panel
  2. Open the Properties panel
  3. 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:

import java.text.DateFormat;
import java.util.Locale;
import java.util.TimeZone;
 
import net.sf.jasperreports.engine.util.DefaultFormatFactory;
 
/**
 * Delegates creation of date and number formatters to JasperReports' default
 * formatters. This class ensures that dates are formatted consistently across
 * all reports.
 */
public class ReportFormatFactory extends DefaultFormatFactory {
  /**
   * Returns a DateFormat instance that creates dates in YYYY/MM/dd format.
   *
   * @param pattern Unused.
   * @param locale Passed to the DefaultFormatFactory instance.
   * @param timezone Passed to the DefaultFormatFactory instance.
   *
   * @return An object that can format dates.
   */
  @Override
  public DateFormat createDateFormat(
    String pattern, Locale locale, TimeZone timezone ) {
    return super.createDateFormat( "YYYY/MM/dd", locale, timezone );
  }
}

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:

  1. Create a new project folder called i18n
    1. ​Right-click on the project name
    2. Select New >> Folder
    3. Set Folder name to i18n
    4. Click Finish
  2. Right-click on i18n
  3. Select New >> Other
  4. Expand Messages Editor
  5. Select ResourceBundle
  6. Click Next
  7. Set the name to ReportsLocale
  8. Add a Locale (e.g., en_US)
  9. Click Finish

Add the i18n directory to the build process:

  1. Right-click i18n
  2. Select Build Path >> Configure Build Path
  3. Click Add Folder
  4. Check i18n
  5. Click OK
  6. Click OK again

Change the createDateFormat method:

  @Override
  public DateFormat createDateFormat(
    String pattern, Locale locale, TimeZone timezone ) {
    String dateFormat = DATE_FORMAT_DEFAULT;
 
  try {
   ResourceBundle rb = ResourceBundle.getBundle( "ReportsBundle" );
     String df = rb.getString( DATE_FORMAT );
 
     if( df != null ) {
       dateFormat = df;
     }
  }
  catch( Exception e ) {
   // If the resource bundle isn't found, use the default date format.
   // TODO: Pass this exception into a logger.
  }
 
    return super.createDateFormat( dateFormat, locale, timezone );
  }

And add these constants to the class definition (immediately after the publc class declaration, around line 15/16):

  private final static String DATE_FORMAT = "date.format";
  private final static String DATE_FORMAT_DEFAULT = "YYYY/MM/dd";
 
Restart Jaspersoft Studio, then:
 
  1. Edit the ReportsLocale file
  2. Add a date.format property
  3. Set the property value to: dd/MM/YYYY
  4. Set the property value for all locales.

When the report is run, the date should look like 29/02/1976, for example.

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