Thursday 6 November 2014

Disable Past dates and future dates in ADF Input Date

Hi all, In today's post i am going to explain you how to disable past date and future date for Input Date component. Sometime you get requiment to disable the past dates and future date also. This post can help you.

My requirement is to show only today and tomorrow's date and rest all need to disable. User should not select othere then these dates.
There are two attributes in inputDate component MinValue and MaxValue.

1. MinValue attribute is used to disable the back dates.

2. MaxValue attribute is used to disable the forward dates.

I have create one bean and two methods as show below

  private Date minDate = new Date();

    public Date getMaxDateVal() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 1); 
        return cal.getTime();

    }

cal.add(Calendar.DATE, 1);  // how many forwarding dates need to enable, In my requirement i should show tomorrow date, sho i have mentioned "1".


    public Date getMinDate() {
    try {
                Calendar cal = Calendar.getInstance();
    java.util.Date date = cal.getTime();
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String currentDate = formatter.format(date);
    minDate = formatter.parse(currentDate);
  
    return formatter.parse(currentDate);
            } catch (Exception e) {
    return null;
            }
        }


jspx page code is  below.

 <af:inputDate label="Label 1" id="id1"
                      binding="#{viewScope.DateBean.testdate}"
                      maxValue="#{viewScope.DateBean.maxDateVal}"
                      minValue="#{viewScope.DateBean.minDate}"/>

 Run the page.


Thanks :)  Happy Learning :)







No comments:

Post a Comment