Friday, May 25, 2007

A simple java web calendar

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * A simple java web calendar
 * 
 */
public class CalendarBuilder
{
  protected String weeks[] =
  { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
    "Friday", "Saturday" };
  protected String shortWeeks[] =
  { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };

  protected String cnWeeks[] =
  { "日", "一", "二", "三", "四", "五", "六" };

  protected int DaysInMonth[] =
  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  private Locale locale = null;

  public CalendarBuilder()
  {
    this.locale = Locale.CHINA;
  }

  public CalendarBuilder(Locale locale)
  {
    this.locale = locale;
  }

  public String getCalendar(Date date)
  {
    StringBuffer sb = new StringBuffer();
    if (locale.equals(Locale.CHINA))
    {
      for (String week : cnWeeks)
      {
        sb.append(week + " ");
      }
    } else
    {
      for (String week : shortWeeks)
      {
        sb.append(week + "\t");
      }
    }
    sb.append("\n");

    Calendar cal = Calendar.getInstance(locale);
    cal.setTime(date);

    int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    /* System.out.println("days:" + days);*/
    int curDay = cal.get(Calendar.DAY_OF_MONTH);
    int curYear = cal.get(Calendar.YEAR);
    int curMonth = cal.get(Calendar.MONTH);

    cal.add(Calendar.DAY_OF_MONTH, 1 - curDay);

    int dayOfFirstWeek = cal.get(Calendar.DAY_OF_WEEK);

    cal.setTime(new Date());
    int theYear = cal.get(Calendar.YEAR);
    int theMonth = cal.get(Calendar.MONTH);
    int today = -1;
    if (curYear == theYear && curMonth == theMonth)
    {
      today = cal.get(Calendar.DAY_OF_MONTH);
    }

    int numOfRows = ((days + dayOfFirstWeek) >= 35) ? 6 : 5;
    int day = 1;
    int count = 1;
    for (int r = 1; r <= numOfRows; r++)
    {
      for (int i = 1; i <= 7; i++)
      {
        if (count < dayOfFirstWeek || count >= (days + dayOfFirstWeek))
        {
          /* sb.append("=\t");*/
        } else if (day == today)
        {
          sb.append("[" + day + "]");
          day++;
        } else if (day == curDay)
        {
          sb.append("{" + day + "}");
          day++;
        } else
        {
          sb.append(day);
          day++;
        }
        sb.append("\t");
        count++;
      }
      sb.append("\n");
    }
    return sb.toString();
  }

  private String _cA(String base_url, int year, 
    int month, int day)
  {
    String dayStr = 
      (day < 10) ? "0" + day : String.valueOf(day);
    String monthStr =
      (month < 10) ? "0" + month : String.valueOf(month);

    return "<a href=\"" + base_url + year + monthStr + dayStr
         + "\">" + day + "</a>";
  }

  private String _cA(String base_url, int year, int month,
    int day, String showText)
  {
    String dayStr = 
      (day < 10) ? "0" + day : String.valueOf(day);
    String monthStr =
      (month < 10) ? "0" + month : String.valueOf(month);

    return "<a href=\"" + base_url + year + monthStr + dayStr
      + "\">" + showText + "</a>";
  }

  public String getNowCalendar()
  {
    return getCalendar(new Date());
  }

  /**
   * 
   * @param date 
   * @param base_url
   * @return
   */
  public String getWebCalendar(Date date, String base_url)
  {
    Calendar cal = Calendar.getInstance(locale);
    cal.setTime(date);
    int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    int curDay = cal.get(Calendar.DAY_OF_MONTH);
    int curYear = cal.get(Calendar.YEAR);
    int curMonth = cal.get(Calendar.MONTH);

    String _TD = "<td>";
    String TD_ = "</td>";
    String _CUR_TD = "<td class=\"cal-on\">";
    String _TODAY_TD = "<td class=\"cal-today\">";

    StringBuffer sb = new StringBuffer();
    /* header of calendar*/
    sb.append("<table id=\"calendar\"><tr>");
    sb.append("<td colspan=\"7\" align=\"center\"><table");
    sb.apend(" width=\"100%\" class=\"cal-header\">");
    sb.append("<tr><td>");

    cal.add(Calendar.MONTH, -1);
    sb.append(_cA(base_url, cal.get(Calendar.YEAR), 
      cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), "<"));
    sb.append("</td><td align=\"center\">");
    sb.append(curYear).append("-").append(curMonth + 1);
    sb.append("</td><td align=\"right\">");
    cal.add(Calendar.MONTH, 2);
    sb.append(_cA(base_url, cal.get(Calendar.YEAR), 
      cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), ">"));
    sb.append("</td></tr></table></td>");
    sb.append("</tr><tr class=\"cal-dayname\">");
    if (locale.equals(Locale.CHINA))
    {
      for (String week : cnWeeks)
      {
        sb.append(_TD + week + TD_);
      }
    } else
    {
      for (String week : shortWeeks)
      {
        sb.append(_TD + week + TD_);
      }
    }
    sb.append("</tr>\n");

    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, 1 - cal.get(Calendar.DAY_OF_MONTH));

    int dayOfFirstWeek = cal.get(Calendar.DAY_OF_WEEK);

    cal.setTime(new Date());
    int theYear = cal.get(Calendar.YEAR);
    int theMonth = cal.get(Calendar.MONTH);
    int today = -1;
    if (curYear == theYear && curMonth == theMonth)
    {
      today = cal.get(Calendar.DAY_OF_MONTH);
    }

    /*count number of rows*/
    int numOfRows = ((days + dayOfFirstWeek) >= 35) ? 6 : 5;
    int day = 1;
    int count = 1;
    curMonth = curMonth + 1;
    for (int r = 1; r <= numOfRows; r++)
    {
      sb.append("<tr>");
      for (int i = 1; i <= 7; i++)
      {
        if (count < dayOfFirstWeek ||
          count >= (days + dayOfFirstWeek))
        {
          sb.append(_TD + TD_);
        } else if (day == today)
        {
          sb.append(_TODAY_TD + _cA(base_url, curYear, curMonth, day) + TD_);
          day++;
        } else if (day == curDay)
        {
          sb.append(_CUR_TD + day + TD_);
          day++;
        } else
        {
          sb.append(_TD + _cA(base_url, curYear, curMonth, day) + TD_);
          day++;
        }
        count++;
      }
      sb.append("</tr>\n");
    }
    sb.append("</table>");
    return sb.toString();
  }

  public static void main(String[] args)
  {//test
    System.out.println(cb.getNowCalendar());
    System.out.println(
      cb.getWebCalendar(d, "http://www.example.com/web_calendar.jsp?date="));
  }
}
 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory