Friday, July 15, 2011

Rendering Current Date in inputDate component.

Currently I am working on a selection criteria screen where the values selected from the choice lists are stored in a session bean which is later on used to filter the queries of other screens. On of those component is inputDate which we have used to enable the user to enter transaction (business unit) creation dates. We are using 2 inputDate component, 1 for Creation Date (From) and the other for Creation Date (To) to see the transaction between from and to dates. The Creation Date (From) is mandatory on selection screen, whereas Creation Date (To) is not. The requirement is that if Creation Date (To) is not given, it should default to current date. As inputDate component does not provide any such property to show current date, we need to do it ourselves. So here is the piece of code to do that:

    public String getMsgCreationDateTo() {
        if(msgCreationDateTo!= null && (!msgCreationDateTo.equals("")))
          return msgCreationDateTo;
        else{
          Date convertedDate = new Date(Date.getCurrentDate());
          String convertedDateString;
          java.text.SimpleDateFormat displayDateFormat = new java.text.SimpleDateFormat ("MM/dd/yyyy");
          convertedDateString = displayDateFormat.format(convertedDate.dateValue());
          return convertedDateString;
        }
    }

Here the default Date class is from oracle.jbo.domain.Date. So if user enters any Creation Date (To) than that value is used, else current date is defaulted in that component, and user does not need to enter that if he wants to see records from 'some day' till today. Here is how it looks like when the page is rendered:


And here is the piece of code to show yesterday's date as From Creation Date ( Current Date - 1 ) :

public String getMsgCreationDateFrm() {
        if (msgCreationDateFrm != null && (!msgCreationDateFrm.equals("")))
            return msgCreationDateFrm;
        else {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.DATE, -1);
            java.text.SimpleDateFormat displayDateFormat = new java.text.SimpleDateFormat("M/dd/yyyy");
            msgCreationDateFrm = displayDateFormat.format(now.getTime() );
            return msgCreationDateFrm;
        }
}


Here is how the output looks like:


In addition, incase you need to convert from yyyy/mm/dd to dd/mm/yyyy format and you are using oracle.jbo.domain.Date data type, then you can use the following piece of code to do that:

        Calendar calendar = Calendar.getInstance();
        oracle.jbo.domain.Date date = new oracle.jbo.domain.Date( new Timestamp( calendar.getTime().getTime() ) );
        System.out.println("Date = " + date);
        DefaultDateFormatter ddf = new DefaultDateFormatter();
        try{
           
            String dateVal = ddf.format("MM/dd/yyyy", date);
            System.out.println("New Date Val = " + dateVal);
        }catch(Exception e){
            e.printStackTrace();
        }

Here is the output:





JDev Release 11.1.1.4 & 11.1.2.

No comments:

Post a Comment