Generating a calendar in JSF and Facelets

First, create a method called something like generateCalendar that will be called from your bean’s getter method:

public void generateCalendar(){
calendar = new ArrayList>();

GregorianCalendar c = new GregorianCalendar();
// If today isn’t Sunday, set start date to the current week’s Sunday
if(c.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
startDate = c.getTime();

c.add(Calendar.MONTH, getCalendarMonths());
endDate = c.getTime();

Date current = startDate;

// Add the days to the week, and the weeks to the calendar
while (current.before(endDate) || dateEquals(current, endDate)) {
List week = new ArrayList();
for(int i = 0; i < 7; ++i) { TrainingCalendarDay day = new TrainingCalendarDay(current); week.add(day); c.add(Calendar.DAY_OF_WEEK, 1); current = c.getTime(); } calendar.add(week); }

private Boolean dateEquals(Date start, Date end) {
Calendar startCalendar = new GregorianCalendar();
Calendar endCalendar = new GregorianCalendar();
return startCalendar.get(Calendar.DAY_OF_MONTH) == endCalendar.get(Calendar.DAY_OF_MONTH)
&& startCalendar.get(Calendar.MONTH) ==
endCalendar.get(Calendar.MONTH) &&
startCalendar.get(Calendar.YEAR) ==

Then, in your xhtml file (using Facelets), you can call your bean’s getter method in a ui:repeat tag, using “week” as the var, and then have a nested ui:repeat tag on the week using “day” as the var.


About buffalobillion

Web Developer, JavaScript Balrog, Java dude, Ruby/Rails enthusiast. Guitar Playa.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s