Web Calendar : HTML Output « Servlets « Java






Web Calendar

     
/*******************************************************************************
 * Copyright (c) 2004 BlueOxygen Technology.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the BlueOxygen Software License v1.0
 * which accompanies this distribution, and is available at
 *
 * Contributors:
 *     BlueOxygen Team - initial API and implementation
 *******************************************************************************/


import java.util.*;
import java.text.SimpleDateFormat;

/**
 * Class WebCalendar
 */
public class WebCalendar
{
  // Constants
  //*----------------------------------------------------------------------*//
  
  // Member Variables
  //*----------------------------------------------------------------------*//
  
  /** */
  protected GregorianCalendar m_gc;
  
  /** */
  protected SimpleDateFormat m_sdf;
  
  // Public Methods and Accessors
  //*----------------------------------------------------------------------*//
  
  /** */
  public WebCalendar()
  {
    m_gc = new GregorianCalendar();
    m_sdf = new SimpleDateFormat();
  }
  
  /** */
  public void setYear (int _iYear) { m_gc.set(Calendar.YEAR,_iYear); }
  
  /** */
  public void setMonth (int _iMonth) { m_gc.set(Calendar.MONTH,_iMonth); }
  
  /** */
  public String getMonth()
  {
    m_sdf.applyPattern("MMMM");
    return m_sdf.format(m_gc.getTime()).toString();
  }
  
  /** */
  public GregorianCalendar getCalendar() { return m_gc; }
  
  /** */
  public String[]
    getDays()
  {
    String days[] = new String
      [m_gc.getMaximum(Calendar.DAY_OF_WEEK) - m_gc.getMinimum(Calendar.DAY_OF_WEEK) + 1];
    m_gc.set(Calendar.DAY_OF_WEEK,1);
    
    for (int i=0; i<days.length; i++) {
      days[i] = m_sdf.format(m_gc.getTime()).toString();
      m_gc.roll(Calendar.DAY_OF_WEEK,true);
    }
    
    return days;
  }
  
  /** */
  public String
    renderOneMonth (String _sTableWidth,
            String _sCellWidth)
            //PrintWriter _out)
  {
    StringBuffer sb = new StringBuffer();
    //out = response.getWriter();
    
    sb.append("<table border='1' width='").append(_sTableWidth).append("'")
    .append(" cellspacing='0' cellpadding='0'>")
    .append("<tr><td align='center' colspan='7'>").append(getMonth())
    .append("</td></tr>")
    .append("<tr>");
    
    String days[] = getDays();
    for (int i=0; i<days.length; i++) {
      sb.append("<td width='").append(_sCellWidth).append("'")
      .append(" align='center'>").append(days[i]).append("</td>");
    }
    
    sb.append("</tr>");
    
    m_gc.set(Calendar.DAY_OF_MONTH,1);
    boolean finish = false;
    
    for (;;) {
      sb.append("<tr>");
      
      for (int i=0; i<days.length; i++) {
        Date t = m_gc.getTime();
        m_sdf.applyPattern("EEEE");
        
        if (days[i].equals(m_sdf.format(t).toString())) {
          m_sdf.applyPattern("d");
          sb.append("<td align='center'>")
          .append(m_sdf.format(t).toString()).append("</td>");
          
          if (m_gc.get(Calendar.DAY_OF_MONTH) == m_gc.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            finish = true;
            break;
          }
          
          m_gc.roll(Calendar.DAY_OF_MONTH,true);
        }
        else {
          sb.append("<td align='center'>&nbsp;</td>");
        }
      }
      
      sb.append("</tr>");
      if (finish) break;
    }
    
    sb.append("</table>");
    
    return sb.toString();
  }
  
  
}

   
    
    
    
    
  








Related examples in the same category

1.Servlet Output HTML Demo
2.Servlet Display Static HTML
3.Prints a conversion table of miles per gallon to kilometers per liter
4.Servlet: Print Table
5.Html utilities
6.Html Parse Servlet
7.Escape and unescape string
8.Escapes newlines, tabs, backslashes, and quotes in the specified string
9.HTML Helper
10.Escape HTML
11.Convert HTML to text
12.Text To HTML
13.Unescape HTML
14.Java object representations of the HTML table structure
15.Entity Decoder
16.Format a color to HTML RGB color format (e.g. #FF0000 for Color.red)
17.Definitions of HTML character entities and conversions between unicode characters and HTML character entities
18.Encode special characters and do formatting for HTML output
19.HTML color names
20.Utility methods for dealing with HTML
21.Filter the specified message string for characters that are sensitive in HTML
22.A collection of all character entites defined in the HTML4 standard.
23.Decode an HTML color string like '#F567BA;' into a Color
24.Normalize Post Data
25.Get HTML Color String from Java Color object
26.HTML Decoder
27.HTML Parser
28.HTML color and Java Color
29.HTML form Utilites
30.Html Dimensions
31.break Lines with HTML
32.insert HTML block dynamically
33.Convert an integer to an HTML RGB value
34.Convert to HTML string