4 Replies Latest reply: May 17, 2012 12:44 AM by Alexandar RSS

    Converting RichInputDate to specific format

    880439
      Hi All,
      greetings!!!

      I am using jdev 11.1.2.1.0
      I have two input date component's on my JSPX.
      <af:inputDate label="From Date" id="fromDate" binding="#{DepositeLog.dtFrom}" autoSubmit="true" immediate="true">
              <af:convertDateTime pattern="#{resourceBundle['Common.shortDisplayDatePattern']}"/>
      </af:inputDate>
      <!--  shortDisplayDatePattern is dd-MMM-yyyy : from Resource Bundle -->
      <af:inputDate label="To Date" id="toDate" binding="#{DepositeLog.dtTo}"  autoSubmit="true" immediate="true">
              <af:convertDateTime pattern="#{resourceBundle['Common.shortDisplayDatePattern']}"/>
      </af:inputDate>
      In managed bean I am getting these dates as,
              System.out.println(this.getDtFrom().getValue());
              System.out.println(this.getDtTo().getValue()); 
         //Output of these 2 SOP's is,        
              Fri May 09 10:18:03 IST 2008
              Tue May 01 10:18:03 IST 2012
            
      Now, I want to convert these dates in the format : 09-May-2008 and 01-May-2012 i.e. in dd-MMM-yyyy format
      These new dates should be of String data type.

      I don't want to manually fetch the required strings with help of split()

      I tried for this,
              this.getDtFrom().getMinValue();
              //which actually returns java.util.Date(), but in my case it returned null.
      Also,
      I tried this,
              SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy");//line 1
              String toDate = this.getDtFrom().getValue().toString();//line 2
              System.out.println(sdf.format(toDate));//line 3
         //But it thrown exception: 
              "java.lang.IllegalArgumentException: Cannot format given Object as a Date" for line #3
      Appreciate your help...

      Thanks and Regards,
      Madhav K.

      Edited by: Madhav on May 16, 2012 10:28 PM
        • 1. Re: Converting RichInputDate to specific format
          Mohammad Jabr
          import oracle.adf.view.rich.component.rich.input.RichInputDate;
          
          public class Test
          {
            private RichInputDate fromDate;
          
            public Test()
            {
            }
          
            public void setFromDate(RichInputDate fromDate)
            {
              this.fromDate = fromDate;
            }
          
            public RichInputDate getFromDate()
            {
              return fromDate;
            }
          
            public String readDate()
            {
              // Add event code here...
               java.util.Date fromDate = (java.util.Date) this.getFromDate().getValue();
               System.out.println(fromDate);
               System.out.println(formatUtilDateAsSimpleDate(fromDate));
              return null;
            }
          
            public  String formatUtilDateAsSimpleDate(java.util.Date date)
            {
              java.text.SimpleDateFormat sdf =
                new java.text.SimpleDateFormat("dd-MMM-yyyy");    
              return sdf.format(date);
            }
          }
          The result is
          Tue May 01 00:00:00 GMT 2012
          01-May-2012
          Edited by: Mohammad Jabr on May 17, 2012 8:30 AM
          • 2. Re: Converting RichInputDate to specific format
            prateekazam
            The following code help you to see the date on ui on given pattern which is defined in <af:convertDateTime>


            <af:inputDate label="From Date" id="fromDate" binding="#{DepositeLog.dtFrom}" autoSubmit="true" immediate="true">
            <af:convertDateTime pattern="#{resourceBundle['Common.shortDisplayDatePattern']}"/>
            </af:inputDate>

            Although in backing bean if you are getting the value of date that will not formatted.

            BTW you want to format the date on backing bean.Step are

            1-Create the object of SimpleDateFormat
            2-call format method on newly created object
            3-the method will return sting value of the formatted date

            Code example is following
                public void dateValueChange(ValueChangeEvent valueChangeEvent) {
                    System.out.println(datevalue.getValue());
                    SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy");
                   String date= sdf.format(datevalue.getValue());
                   System.out.println("=========="+date);
                   }
            • 3. Re: Converting RichInputDate to specific format
              880439
              Hi Mohammad,
              Thanks for your help...
              It worked!!!!!!!


              Thanks and Regards,
              Madhav K.
              • 4. Re: Converting RichInputDate to specific format
                Alexandar
                Problem is in line 2:
                SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy");//line 1
                String toDate = this.getDtFrom().getValue().toString();//line 2
                System.out.println(sdf.format(toDate));//line 3
                you are trying to format string.
                Try it like this:
                SimpleDateFormat sdf = new SimpleDateFormat("dd-mmm-yyyy");//line 1
                System.out.println(sdf.format(this.getDtFrom().getValue()));//line 3