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;
    }

Comments:

How did you get the labels "Choose Start Date:" and "End Calendar:" to be horizontally aligned with the Calendar?

Posted by Ray Santiago on February 22, 2006 at 02:53 AM PST #

Select the compnent and use Shift-Arrow to move the component in small increments (where Arrow = up, down, left, right arrow). Alternatively, select them both and choose Align > Right from the pop-up menu.

Posted by Diva 2 on February 22, 2006 at 08:52 AM PST #

Post a Comment:
Comments are closed for this entry.
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