Problems with viewing calendar


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Problems with viewing calendar

  1. #1
    Join Date
    Mar 2006
    Posts
    2

    Question 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):
    Code:
        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_cale...w_problems.jpg
    http://www.recap13.com/ANNAT/09_cale...t_problems.jpg

    Thanks in advance!
    Last edited by Torwald; 03-16-2006 at 06:49 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    England
    Posts
    10

    Post Solution to 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!

    Code:
        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();
      	}

  3. #3
    Join Date
    Mar 2006
    Posts
    2

    Exclamation

    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!

Similar Threads

  1. convert String to Calendar
    By Force Flow in forum Java
    Replies: 4
    Last Post: 06-29-2006, 02:01 PM
  2. Replies: 4
    Last Post: 06-30-2005, 04:43 PM
  3. Hai how to Display text in a Calendar Control
    By itchock in forum VB Classic
    Replies: 0
    Last Post: 06-26-2005, 09:22 AM
  4. Problems with free/busy calendar information
    By Hanna Talikka in forum Enterprise
    Replies: 3
    Last Post: 08-29-2002, 11:44 AM
  5. Enterprise-wide Office Calendar (Outlook & Exchange)
    By CatStraub in forum Enterprise
    Replies: 0
    Last Post: 06-13-2000, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center