Creating a New Date Format

By default, Canvas Technology platform provides the following four date formats:

  1. MM/dd/yyyy (e.g. 09/22/2009)
  2. dd/MM/yyyy (e.g. 22/09/2009)
  3. dd-MM-yyyy (e.g. 22-09-2009)
  4. MM-dd-yyyy (e.g. 09-22-2009)

Perform the following steps to create a custom date format:

  1. Create an XML file (e.g. cuser-dateformats.xml) under resources package for maintaining the custom date formats.
  2. At the Initialization sequence where you initialize the Canvas configuration, you must do the registration of the custom Date format. For example, if there is a StartTaskServlet class that initializes the application, add the initializeDateFormats() method. This method registers the custom Date formats into the registry by calling the registry() method of DateFormatRegistry with source file (e.g. cuser-dateformats.xml) as a parameter.

    import com.intellectdesign.canvas.pref.date.DateFormatRegistry;
    
    private void initializeDateFormats()
    {
    	try
    	{
        	DateFormatRegistry dateFormatRegistry = DateFormatRegistry.getInstance();
        	dateFormatRegistry.register("cuser-dateformats.xml");
    	} 
    	catch (BaseException e)
    	{
        	LOGGER.error("Unable to start DateFormatRegistry", e);
    	}
    }
  3. In the cuser-dateformats.xml file, a new format is to be defined as:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <canvas-formats type="date">
    
    	<format 
    		Id = "dd-MM-yyyy" 
    		description = "dd-MM-yyyy" 
    		formatter-class = "com.intellectdesign.modelhouse.pref.CustomerDateFormatter" 
    		canOverride = "true" 
    		isEnabled = "true"
    	/>
    
    	<format 
    		Id = "dd,MM,yyyy" 
    		description = "dd,MM,yyyy" 
    		formatter-class = "" 
    		canOverride = "true" 
    		isEnabled = "true"
    	/>
    
    	<format 
    		Id = "dd-MM-yyyy" 
    		description = "dd-MM-yyyy" 
    		isEnabled = "false"
    	/>
    
    </canvas-formats>

    Here, the dd-MM-yyyy format is overridden by the new definition and is disabled for displaying in the Combo Box in the Update Preferences window.

  4. Define a date format in the cuser-dateformats.xml file as a combination of the following factors:
    1. Configure the new format with the Id and description attributes that are unique for that particular format (mandatory inputs).

      Id - The format is identified by its unique ID.
      description - Description for that format that is displayed in the user preferences form.

    2. Configure the javaDateFormat, which is the Java standard date format. Here, some of the Java date variables are represented as:

       Format								Description														 Values
      ---------			----------------------------------------------------------------				---------
      D						Day of the month, 2 digits without leading zeros							1 to 31
      Dd						Day of the month, 2 digits with leading zeros								01 to 31
      M						Numeric representation of a month, without leading zeros					1 to 12
      MM						Numeric representation of a month, with leading zeros						01 to 12
      MMM						A short textual representation of a month									Jan to Dec
      MMMM					A full textual representation of a month									January to December
      
      y						A two digit representation of a year										99 or 03
      yy						A two digit representation of a year										99 or 03
      yyy						A two digit representation of a year										99 or 03
      yyyy					A full numeric representation of a year, 4 digits							1999 or 2003
    3. Configure the formatter-class for the server-side formatting in case of print and export. If this is provided, then the provided formatter class has higher priority over the Canvas Framework default formatter class. This is an optional input. By default framework takes care of the formatting. This class has to specified with the full path including the package:

      For example, CustomerDateFormatter class can been specified in the package called com.intellectdesign.modelhouse.pref. The path can be specified as:

      formatter-class = "com.intellectdesign.modelhouse.pref.CustomerDateFormatter"

      This formatter-class must implement the IdateFormatter class. Inside this class one method called “formatDate” must be implemented to format the date into the user preferred format.

    4. Configure the canOverride value as "true" or "false" for any date format. The default value is false. This attribute indicates whether a definition of the format can be overridden. If it is true, then the definition of the format is overridden with the new format definition.

    5. Configure the isEnabled value as "true" or "false" for any date format. The default value is true. If it is true, then the format is displayed in the Combo Box or it is not displayed in the user preferences form, but the format definition is stored in the registry.