7.23. Formatter

7.23.1. Function overview

Provides a function to format data such as date and number, and convert it to string type. By consolidating the format configuration in this function, configuration is not necessary for each format such as screen, file and email.

7.23.2. Module list

<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-core</artifactId>
</dependency>

7.23.3. How to use

7.23.3.1. Configure the formatter

This function can use the formatter that the framework supports by default even without any configuration.

To change the default format pattern or add a formatter, refer to Change the formatter configuration and add the configuration to the system repository.

7.23.3.2. Use the formatter

Use FormatterUtil for formatting.

The formatter has a separate name from the class name to specify the formatter to be used.

When calling FormatterUtil.format, formatter name, format target, and format pattern are specified, and an appropriate formatter is selected based on the format name and data type to be formatted.

Formatted using the selected formatter and specified format pattern. If the format pattern is not specified explicitly, the default pattern configured for each formatter is used.

Implementation examples

// Format using the default pattern
// Specify the name of the formatter to use for the first argument
// Specify the value to format in the second argument
FormatterUtil.format("dateTime", input);

// When specifying a pattern for formatting
// Specify the first and second arguments in the same way as the default pattern
// Specify the format pattern to use for the third argument.
FormatterUtil.format("dateTime", input, "yyyy/MM/dd");

The formatter provided by default with this function are shown below:

Formatter name Type of data to format Default format pattern Remarks
dateTime Date yyyy/MM/dd  
dateTime String yyyy/MM/dd Requires a pattern to format the date string (default yyyyMMdd )
number Number #,###.###  
number String #,###.###  
dateTime

Formatter for formatting dates.

The types to be formatted are Date , its derived classes and String . For the pattern, specify the syntax specified by SimpleDateFormat. The default pattern is yyyy/MM/dd .

To format String , the pattern of the date string to be formatted should also be configured. The date string pattern to be formatted is yyyyMMdd by default. To change the configuration, refer to Change the formatter configuration .

number

Formatter for formatting numerics.

The types to be formatted are Number , its derived classes and String . For the pattern, specify the syntax specified by DecimalFormat. The default pattern is #,###.### .

Usage examples

For example, to output to a file using data binding with this function, use it with getter of Bean.

import java.util.Date;

public class SampleDto {
    private Date startDate;
    private Integer sales;

    // Create a getter to acquire the formatted string
    public String getFormattedStartDate() {
        return FormatterUtil.format("dateTime", startDate);
    }

    public String getFormattedSales() {
        return FormatterUtil.format("number", sales, "#,### Yen");
    }

    // Other setters and getters are omitted.
}

7.23.4. Change the formatter configuration

The following procedures are required to change the formatter configuration.

Configure nablarch.core.text.FormatterConfig in the component configuration file.

Point
  • The component name should be formatterConfig .

Configure the formatter list used for nablarch.core.text.FormatterConfig . The property name of the list should be formatters .

The default format configuration supported by the framework is shown below.

<component name="formatterConfig" class="nablarch.core.text.FormatterConfig">
  <!-- List holding formatters -->
  <property name="formatters">
    <list>
      <component class="nablarch.core.text.DateTimeFormatter">
        <!-- Name to use when calling the formatter -->
        <property name="formatterName" value="dateTime" />
        <!-- Configure the default format pattern -->
        <property name="defaultPattern" value="yyyy/MM/dd" />
      </component>
      <component class="nablarch.core.text.DateTimeStrFormatter">
        <property name="formatterName" value="dateTime" />
        <property name="defaultPattern" value="yyyy/MM/dd" />
        <!-- Configuration of the properties that represent the date string pattern
             is also required for the formatter of date string -->
        <property name="dateStrPattern" value="yyyyMMdd" />
      </component>
      <component class="nablarch.core.text.NumberFormatter">
        <property name="formatterName" value="number" />
        <property name="defaultPattern" value="#,###.###" />
      </component>
      <component class="nablarch.core.text.NumberStrFormatter">
        <property name="formatterName" value="number" />
        <property name="defaultPattern" value="#,###.###" />
      </component>
    </list>
  </property>
</component>

Important

When changing the default formatter configuration in component definition, configuration for formatters and properties that do not change should be described. Formatters not described in the component definition cannot be used.

7.23.5. Adding formatter

The following steps are required to add a formatter.

  1. Create the implementation class Formatter .
Format process is performed by the class that implements Formatter .
  1. Add the formatter configuration created in the component configuration file

Configure nablarch.core.text.FormatterConfig and format list to the component configuration file by refering to Change the formatter configuration .

<component name="formatterConfig" class="nablarch.core.text.FormatterConfig">
  <property name="formatters">
    <list>
      <!-- Default formatter -->
      <component class="nablarch.core.text.DateTimeFormatter">
        <property name="formatterName" value="dateTime" />
        <property name="defaultPattern" value="yyyy/MM/dd" />
      </component>
      <component class="nablarch.core.text.DateTimeStrFormatter">
        <property name="formatterName" value="dateTime" />
        <property name="defaultPattern" value="yyyy/MM/dd" />
        <property name="dateStrPattern" value="yyyyMMdd" />
      </component>
      <component class="nablarch.core.text.NumberFormatter">
        <property name="formatterName" value="number" />
        <property name="defaultPattern" value="#,###.###" />
      </component>
      <component class="nablarch.core.text.NumberStrFormatter">
        <property name="formatterName" value="number" />
        <property name="defaultPattern" value="#,###.###" />
      </component>
      <!-- Added formatter -->
      <component class="sample.SampleFormatter">
        <property name="formatterName" value="sample" />
        <property name="defaultPattern" value="#,### Yen" />
      </component>
    </list>
  </property>
</component>