Sunday, August 15, 2010

Working with af:inputDate in Bean

af:inputDate component register itself as instance of oracle.adf.RichInputDate class and its value attribute accepts java.util.Date type. Details of this component could be obtained at TagDoc.

Date (java.util.Date) itself does not have format, we format its value when we do need to print it. In order to format it in UI you could use af:convertDateTime but if you are thinking how can we do that in bean then here is a sample. Have a method something like below for the value change listener:

    public void valuechangelstnr(ValueChangeEvent vce){
        java.util.Date dateVal = (java.util.Date)vce.getNewValue();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String datetime = dateFormat.format(dateVal);
        System.out.println("Date Time: " + datetime);
    }

In case you want to set a string value to java.util.Date.. here is the sample:

DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
java.util.Date date = new java.util.Date ();
String dateStr = dateFormat.format (date);
try{
Date date2 = dateFormat.parse (dateStr);
}catch(ParseException pe){
      // do something
}

3 comments:

  1. It wont be a good idea to hard code Date format in the code. It would be better to get it from a resource bundle

    ReplyDelete
  2. I searched for and got this page :) :D shabash ninja :D

    ReplyDelete