5 Replies Latest reply on Jan 30, 2013 7:15 PM by Dimitar Dimitrov

    inputDate  : adding custom convertor


      inputDate component is returning the date in "MM/dd/yyyy" format, if I don't attach any converter to it.
      <af:inputDate label="Date of Birth" id="id1"/>           
      But it is returning the date in long format(ex: Wed Jan 30 00:00:00 UTC+0530 2013) when I add a converter to it. I have created a customDateConverter by extending DateTimeConverter to take care of formatting display and store date formats in getAsObject() and getAsString() methods.
       <af:inputDate label="Date of Birth">
                      <f:converter converterId="customDateConverter"/>
      Why it is returning date in long format when I add a custom converter? How can I control the calender date format when a custom converter is added?
      Thanks in advance for your help

      Edited by: user13331987 on Jan 30, 2013 6:31 AM
        • 1. Re: inputDate  : adding custom convertor
          Dimitar Dimitrov
          It is not necessary to use a custom converter at all. You can use the built-in <af:convertDateTime> converter, for example:
          <af:inputDate label="Date of Birth" autoSubmit="true" id="id1">
            <af:convertDateTime type="date" pattern="dd/MM/yyyy"/>
          Set the format mask you want in the converter tag's "pattern" attribute. Have in mind that if you use the "pattern" attribute, then the conversion is applied on the server (but not on the client), so you should submit the field to the server (e.g. by setting autoSubmit="true") if you want the new value to be formatted immediately upon user enters a new value and exits the field.

          Alternatively, you can skip converter's "pattern" attribute if you use "dateStyle" attribute, for example:
          <af:inputDate label="Date of Birth" id="id1">
            <af:convertDateTime type="date" dateStyle="short"/>
          As far as "pattern" attribute is not used in this case, the framework performs conversion automatically both at server-side and at client-side, so it is not necessary to set autoSubmit="true" in order to format the new value immediately upon the user enters a new value and exits the field.

          Have a look at the documentation of <af:comvertDateTime> for more details.

          1 person found this helpful
          • 2. Re: inputDate  : adding custom convertor
            Thanks Dimitar , for quick response.

            My requirement is date display format is different from storage format. EX: need to display date in "MM-dd-yyyy" format but while storing it need to convert to "yyyy-MM-dd" format.
            I am adding custom converter to convert display format to storage format and vice versa with the help of getAsObject() and getAsString() methods.

            Can we meet this requirement without custom converter?

            • 3. Re: inputDate  : adding custom convertor
              Dimitar Dimitrov
              What do you mean when you say "storage format"? Where do you store the date value which is entered in the input field?

              <af:inputDate> components accept values of type java.util.Date (internal data values of these components are of this type), they do not accept String values.

              java.util.Date objects do not have a format, they are just date/time values. String representations of date/time values (which are displayed on the screen) have got formats, however. If you persist the value in a database column of DATE type, then you do not have to do any character formatting on the value. But if you have to persist the value in a character format (e.g. in a text file or in a database column of some character type) then you should get the java.util.Date value from the <af:inputDate> component and convert it to the necessary character representation. A JSF converter would not help in this case because JSF converters have a different purpose (e.g. they are used to convert an internal value of a JSF component to a string that is displayed on the screen, and in the opposite direction - to parse the string value entered in the field to an internal value).

              If you have to convert a java.util.Date object to a character string with a specific format mask, then you could use a standard Java data/time formatter - have a look at the class java.text.SimpleDateFormat.

              • 4. Re: inputDate  : adding custom convertor
                My requirement is, need to display the user selected calender date in "MM-dd-yyyy" format and when user saves the form I need to save the same date into a xml file in "yyyy-MM-dd" format.
                If user reopens the record need to parse the date value needs to display it in "MM-dd-yyyy" format.

                Thats why I am adding jsf converter to inputDate. But when I add converter calender control returning date in long format(not sure about the exact format).
                • 5. Re: inputDate  : adding custom convertor
                  Dimitar Dimitrov
                  I will try to explain again. What is misunderstood is that you should have two separate conversions - a conversion between XML file's format "yyyy-MM-dd" and a java.util.Date object and a conversion between a java.util.Date object and a string in format "MM-dd-yyyy". The 1st one must be done when you read/write from/to the XML file and load/store the value into/from a some kind of a Model layer. The 2nd one happens between the server-side <af:inputDate> component's local value and the string displayed/entered in the HTML field on the screen during the JSF's APPLY_REQUEST_VALUES phase. The 2nd conversion is performed by a JSF converter, but the 1st one cannot be done using a JSF converter and it has to be done in other way. Please, look at the details below:

                  You must have some kind of a Model layer (for example, a backing bean) that supplies the <af:inputDate> component with a value of java.util.Date datatype.
                  import java.util.Date;
                  public class MyBean {
                    private Date myDateValue;
                    public Date getMyDateValue() {
                      return myDateValue;
                    public void setMyDateValue(Date value) {
                      myDateValue = value;
                  <af:inputDate value="#{MyBean.myDateValue}" ...>
                  The datatype of the value must be java.util.Date, it cannot be java.lang.String because the "value" attribute of <af:inputDate> does not accept String. It is your responsibility to read the necessary date literal (which is in format "yyyy-MM-dd") from the XML file, to convert the string to java.util.Date and to load the value into the corresponding bean property when you navigate to the necessary record. Also it is your responsibility to convert the bean property's java.util.Date value to a string representation in format "yyyy-MM-dd" when storing it back to the XML file. You can do that using a standard Java formatter class (e.g. java.text.SimpleDateFormat) if you read/write the XML file directly in code (e.g. when you do not use any framework for marshaling/unmarshaling XML files). You cannot use a JSF converter here because JSF converters are serve different purpose.

                  Then you must define a JSF converter on the <af:inputDate> component in order to convert the internal value of the JSF component (which is of type java.util.Date) to a readable string representation displayed/entered in the input field on the screen in format "MM-dd-yyyy":
                  <af:inputDate value="#{MyBean.myDateValue}" ...>
                    <af:convertDateTime pattern="MM-dd-yyyy"/>

                  Edited by: Dimitar Dimitrov on Jan 30, 2013 9:06 PM

                  Edited by: Dimitar Dimitrov on Jan 30, 2013 9:12 PM