Wednesday Feb 15, 2006

Calendar Component Tips

Gail (Diva 1) is working on a Using the Calendar Component tutorial. It will be awhile until it gets published (she has lots of irons in the fire) so here are some excerpts from the tutorial.

If you don't set a minimum date, the component defaults to a minimum date of the day after tomorrow. To set your own minimum date you can add code like the following and bind the minDate and maxDate properties to these page bean properties.


private long MILLISECONDS_PER_DAY = 86400000;
private Date today = new Date();

private Date minCalDate;
    
    public Date getMinCalDate() {
        minCalDate = new Date(this.today.getTime()-MILLISECONDS_PER_DAY);
        return minCalDate;
    }
    
    private Date maxCalDate;
    
    public Date getMaxCalDate() {
        Date today = new Date();
        today.setTime(this.today.getTime());
        maxCalDate = new Date(today.getTime() + MILLISECONDS_PER_DAY \* 30);
        return maxCalDate;
    }

If you want to set a default date, you can put code like the following in the prerender method.

public void prerender() {
        // Default start date to today
        if (startCalendar.getSelectedDate() == null) {
            startCalendar.setSelectedDate(this.today);
        }   
    }

If you want to validate starting and ending dates, you can use code like the following (make these be validate event handlers for the Calendar components).

public void endCalendar_validate(FacesContext context, UIComponent component, Object value) {
        Date endDate = (Date)value;
        Date startDate = startCalendar.getSelectedDate();
        if (startDate != null) {
            if (startDate.after(endDate)) {
                throw new ValidatorException(new FacesMessage
                        ("End date must be after start date."));
            } else {
                long nbrDays =
                        ((endDate.getTime() - startDate.getTime()) /
                        MILLISECONDS_PER_DAY) + 1;
                if (nbrDays < 8 ) {
                    throw new ValidatorException(new FacesMessage
("End date must be at least one week after the start date."));
                }
            }
        }
    }

public String validateButton_action() {
            validationMsgTextField.setText(
            (String)DateFormat.getDateInstance(DateFormat.MEDIUM).format(startCalendar.getSelectedDate())
                +
                " - "
                +
                (String)DateFormat.getDateInstance(DateFormat.MEDIUM).format(endCalendar.getSelectedDate())
                + " is a valid date range."); 
        return null;
    }

About

divas

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today