Thursday, August 30, 2012

Dealing with Date format

By default whenever a date field is dragged onto the page, an <af:convertDateTime> component is added to it which decides the pattern and other prerequisites (like Locale,Timezone etc) . So the simplest way to change the display pattern for date is to -

1.Select <af:convertDateTime> component.
2.Edit the Pattern
3.Change it to your required pattern ,say dd/MM/yyyy as in my case.

 
But this has to be done wherever the date field is present , so instead of changing the format on every component it is recommended to be changed from the model layer itself, so that changes reflect at all levels.


Convert Date at Model layer-

Date format can be changed at the Model layer either in EO or VO level attributes. If we change it in the EO level, it gets reflected in VO level as well.
By default on selecting Format Type :Simple Date under UIHints tab in EO for the date attribute, we can select
the required date format from the preconfigured format list provided .


 
But what if we want custom format like dd/MM/yyyy or dd/MMM/yy ?

So in this case though we can add custom pattern on the view page using <af:convertDateTime> (discussed earlier),but it won't be available in case of search queries ie to format the pattern for the bind variables (search fields).
So for such requirement, we need to Locate formatinfo.xml under the following directory( For Jdeveloper 11.1.2.1.0)

C:\Documents and Settings\[USER]\ApplicationData\JDeveloper\system11.1.2.1.38.60.81\o.BC4J\formatinfo.xml

 
Edit formatinfo.xml and modify the required date class:


  1. <DOMAIN CLASS="oracle.jbo.domain.Date">
  2. <FORMATTER name="Simple Date" class="oracle.jbo.format.DefaultDateFormatter">
  3. <FORMAT text="yyyy-MM-dd" />
  4. <FORMAT text="yyyy-MM-dd G 'at' hh:mm:ss" />
  5. <FORMAT text="EEE, MMM d, ''yy" />
  6. </FORMATTER>
  7. </DOMAIN>


After we add required format using <FORMAT> tag the code would look as follows:


  1. <DOMAIN CLASS="oracle.jbo.domain.Date">
  2. <FORMATTER name="Simple Date" class="oracle.jbo.format.DefaultDateFormatter">
  3. <FORMAT text="yyyy-MM-dd" />
  4. <FORMAT text="yyyy-MM-dd G 'at' hh:mm:ss" />
  5. <FORMAT text="EEE, MMM d, ''yy" />
  6. <FORMAT text="dd-MM-yyyy" />
  7. <FORMAT text="dd-MMM-yyyy" />
  8. </FORMATTER>
  9. </DOMAIN>

Now when we browse through our list of Formats in 'UIHints' tab, we can find our custom format pattern available.


So now we can select dd/MM/yyyy as our format for date and the change reflects to all levels .

 
 Yet another way to change the date format would be to use the Locale specific date formatting .i.e without changing individual date attribute's format.
It can be accomplished by adding the following localization property inside trinidad-config.xml (Present under WEB-INF folder of View Controller)


  1. <formatting-locale>nl-BE</formatting-locale>

This would change all date types present in your project to dd/MM/yyyy from the default MM/dd/yyyy . But the only problem with this is it changes all the Numeric formatting types also to the 'nl-BE' locale (IANA-formatted locale). Other IANA-formatted locale for changing date to dd/MM/yyyy is 'en-GB' .

Note: 
<formatting-locale>:  Defines the date and number format appropriate to the selected locale. ADF Faces and Trinidad, will by default, format dates and numbers in the same locale used for localized text. If you want dates and numbers formatted in a different locale, you can use an IANA-formatted locale (for example, ja, fr-CA). The contents of this element can also be an EL expression pointing at an IANA string or a java.util.Locale object.

No comments:

Post a Comment