Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Problems with viewing calendar

I'm working on a calendar project, just a practise thingy to learn java. I borrowed bits of code here and there, and wrote a lot on my own. I've managed to patch together a piece of code that generates html code for the calendar and returns everything as a string.

This is it (the formatter seems to get a bit confused by the html, especially the "p" tags):

AddToArray findEvent = new AddToArray(publicArraylist);
String tableCells(Calendar thisMonth)
{
publicArraylist.add(new String[] {"Meet Nicolas!", "2006.03.15", "20:30", "This is the first record in the arraylist. It can't be empty (NullPointerException)."});
StringBuffer returnValue = new StringBuffer();
//** record the current value of the field Calendar.MONTH
int iThisMonth = thisMonth.get(Calendar.MONTH);
Calendar today = Calendar.getInstance(); //used to highlight today's date cell in the calendar
Calendar endOfMonth = (Calendar)thisMonth.clone();
endOfMonth.set(Calendar.DATE,endOfMonth.getActualMaximum(Calendar.DATE));
endOfMonth.add(Calendar.DATE,7-endOfMonth.get(Calendar.DAY_OF_WEEK));

//** set calendar to first day of the first week (may move back 1 month)
thisMonth.add(Calendar.DATE,1-thisMonth.get(Calendar.DAY_OF_WEEK));

do
{

returnValue.append("<TR BORDER=1 VALIGN=TOP>");
for (int i=0; i < 7; i++)
{
returnValue.append("<TD HEIGHT=100 WIDTH=85");
if (thisMonth.get(Calendar.MONTH) != iThisMonth) returnValue.append(" BGCOLOR=#e0e0e0"); // dark background for out-of-month days
else if (dfDateField.format(thisMonth.getTime()).equals(dfDateField.format(today.getTime()))) returnValue.append(" BGCOLOR=#ffff00"); // yellow background for today's date
else returnValue.append(" BGCOLOR=#ffffff"); // default white background

returnValue.append(">" +
dfDateField.format(thisMonth.getTime()) + ">" +
"<FONT FACE=VERDANA COLOR=#000000 SIZE=2>" + dfDayOfMonth.format(thisMonth.getTime()) + "");
//### SEARCH FOR EVENTS OF THIS DAY AND THEN DISPLAY THEM ###
returnValue.append("<p>");
String[] theEvent = new String[3];
theEvent = findEvent.getEvent(thisMonth.getTime());

if (theEvent[2] != null)
{
returnValue.append(theEvent[2] + ":\n");
returnValue.append("<FONT COLOR=RED>");
returnValue.append(theEvent[0]);
returnValue.append("</FONT>");
}
returnValue.append("</p>");
returnValue.append("</FONT>");
returnValue.append("</TD>");
thisMonth.add(Calendar.DATE,1);
}
returnValue.append("</TR>");
}
while(thisMonth.getTime().before(endOfMonth.getTime()));
//** restore date to first of this month:
thisMonth.add(Calendar.MONTH,-1);
thisMonth.set(Calendar.DATE,1);

return returnValue.toString();
}

And here's the problem:
The beginning of the calendar won't show! Everything from and with the row which includes today. For example, if today is in the very beginning of the month, all of the calendar is shown. If today is one of the last days of the month, only the last row is shown. It is extremely frustrating, and I just can't seem to find the solution. I'd really appreciate of someone would like to take a look at it. Don't hesitate to ask questions about the coding, I'll be glad to try to explain anything (even I think the code is a mess).

Here are two screenshots to illustrate my problem. As you might notice, the output is slightly different than shown in the code. It no longer outputs "no events today" if there are no events to display. Also, there seems to have been a problem with the naming of the files. Naturally, the file called "w problems" is the one that shows the calendar without problems, and vice versa.
http://www.recap13.com/ANNAT/08_calendar_show_events_w_problems.jpg
http://www.recap13.com/ANNAT/09_calendar_show_events_wout_problems.jpg

Thanks in advance!
[4449 byte] By [Torwald] at [2007-11-11 8:14:46]
# 1 Re: Problems with viewing calendar
Hello

Here's some code, based on yours of course, that should sort out your problem. It includes a utility method. Hopefully, the code and the comments will explain what's going on. Of course, you will need to add the days-of-week header row etc. Good luck!

private boolean datesAreEqual(Calendar dateA, Calendar dateB)
{
return dateA.get(Calendar.DATE) == dateB.get(Calendar.DATE)
&& dateA.get(Calendar.MONTH) == dateB.get(Calendar.MONTH)
&& dateA.get(Calendar.YEAR) == dateB.get(Calendar.YEAR);
}

String tableCells(Calendar thisMonth)
{
String[] theEvent;
boolean useFiveRowsWhereAppropriate = true; // max is 6 rows. but can use 5 rows for e.g. March 2006
int iThisMonth = thisMonth.get(Calendar.MONTH); //** record the current value of the field Calendar.MONTH
StringBuffer returnValue = new StringBuffer();

// display a Sunday as first date in Calendar
// this may be a date near the end of the previous month
Calendar firstDayInMonth = Calendar.getInstance();
firstDayInMonth.set(thisMonth.get(Calendar.YEAR), thisMonth.get(Calendar.MONTH), 1);
Calendar firstDateInCalendar = (Calendar) firstDayInMonth.clone();
firstDateInCalendar.add(Calendar.DATE, - (firstDayInMonth.get(Calendar.DAY_OF_WEEK) - 1));

Calendar today = Calendar.getInstance(); //used to highlight today's date cell in the calendar
Calendar dateToDisplay = (Calendar) firstDateInCalendar.clone();

for (int n=0; n<42; ++n) { // up to 6 rows of dates. 7 per row
if (n % 7 == 0) { // if is first day of week, start row
returnValue.append("<TR BORDER=1 VALIGN=TOP>");
}

returnValue.append("<TD HEIGHT=100 WIDTH=85");
if (dateToDisplay.get(Calendar.MONTH) != iThisMonth) returnValue.append(" BGCOLOR=#e0e0e0"); // dark background for out-of-month days
else if (datesAreEqual(dateToDisplay,today)) returnValue.append(" BGCOLOR=#ffff00"); // yellow background for today's date
else returnValue.append(" BGCOLOR=#ffffff"); // default white background

returnValue.append("><FONT FACE=VERDANA COLOR=#000000 SIZE=2>" + dateToDisplay.get(Calendar.DATE));

theEvent = findEvent.getEvent(dateToDisplay.getTime());
returnValue.append("<p>");
if (theEvent!=null && theEvent[2]!=null) {
returnValue.append(theEvent[2] + ":\n");
returnValue.append("<FONT COLOR=RED>");
returnValue.append(theEvent[0]);
returnValue.append("</FONT>");
}
returnValue.append("</p>");
returnValue.append("</FONT>");
returnValue.append("</TD>");

if (n % 7 == 6) { // if is last day of week, close row
returnValue.append("</TR>");
}

// prepare to display next date
dateToDisplay.add(Calendar.DATE,1);

// if at end of 5th row (n==34) and next day to display is in next month,
// then skip 6th row if useFiveRowsWhereAppropriate
if (useFiveRowsWhereAppropriate && n == 34 && dateToDisplay.get(Calendar.MONTH) != iThisMonth) {
break;
}
}
return returnValue.toString();
}
xtsunami at 2007-11-11 22:35:59 >
# 2 Re: Problems with viewing calendar
Woa, thanks a bunch! I played a little bit with your code, and it works perfectly. It's even much more convenient.

I really appreciate your help! It's going to speed things up for me alot.

Thanks again!
Torwald at 2007-11-11 22:37:04 >