LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 981|回复: 12

一个尚待修改的日历类

[复制链接]
发表于 2005-3-7 11:07:31 | 显示全部楼层 |阅读模式
一个尚待修改的日历类
[PHP]
/*
* GetCal.java
*
* Created on 2005年3月7日, 上午9:22
*/

package com.sdate;
import java.util.*;

/**
*
* @author  tony
*/
public class GetCal {
    String days[];
    String strCal;
   
    /** Creates a new instance of getcal */
    public GetCal() {
        days=new String[42];
        for(int i=0;i<42;i++){
            days="";
        }

    }
   
    public String getcalstr(String filename, int mon){
        int mont;
        GregorianCalendar currentDay = new GregorianCalendar();
        int today=currentDay.get(Calendar.DAY_OF_MONTH);
        int month=currentDay.get(Calendar.MONTH);
        int year= currentDay.get(Calendar.YEAR);
        strCal = new String("当前日期:" + year+"年"+ (month+1)+"月"+today+"日");
        
        Calendar thisMonth=Calendar.getInstance();
        if(month-mon >= 0 && month-mon <12){
            thisMonth.set(Calendar.MONTH, month-mon );
            mont = month+1-mon;
        }else{
            thisMonth.set(Calendar.MONTH, month );
            mont = month+1;
            
        }
        thisMonth.set(Calendar.YEAR, year );
        thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
        thisMonth.set(Calendar.DAY_OF_MONTH,1);
        int firstIndex=thisMonth.get(Calendar.DAY_OF_WEEK)-1;
        int maxIndex=thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
        for(int i=0;i<maxIndex;i++)
        {
            days[firstIndex+i]=String.valueOf(i+1);
        }   
           
        strCal = new String(strCal + "<table border=\"0\" width=\"200\" height=\"81\">");
        strCal = new String(strCal + "<div align=center>");
        strCal = new String(strCal + "<tr>");
        strCal = new String(strCal + "<th colspan='7'>");
        strCal = new String(strCal+ "<a href='"+filename+"?mon="+ (mon - 1) +"'><<</a> " + mont +"月" +" <a href='"+filename+"?mon="+ (mon + 1) +"'>>></a>");
        strCal = new String(strCal + "</th>");
        strCal = new String(strCal + "</tr>");
        strCal = new String(strCal + "<tr>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" ><font color=\"red\">日</font></th>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" >一</th>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" >二</th>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" >三</th>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" >四</th>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" >五</th>");
        strCal = new String(strCal + "<th width=\"25\" height=\"16\" ><font color=\"red\">六</font></th>");
        strCal = new String(strCal + "</tr>");
        for(int j=0;j<6;j++) {
            strCal = new String(strCal + "<tr>");
            for(int i=j*7;i<(j+1)*7;i++) {
                strCal = new String(strCal + "<td width=\"15%\" height=\"16\"  valign=\"middle\" align=\"center\">");
                if((i-firstIndex+1)==today){
                    strCal = new String(strCal + "<a href=''><font color=\"red\">"+ days+"</font></a>");
                }else{
                    strCal = new String(strCal + "<a href=''>" + days + "</a>");
                }
            strCal = new String(strCal + "</td>");
            }
            strCal = new String(strCal + "</tr>");
        }
        strCal = new String(strCal + "</div></table> ");
        
        return strCal;        
    }


   
}


[/PHP]
发表于 2005-3-7 13:33:37 | 显示全部楼层
你没有考虑时区。这样,到了美国这样的国家多时区,这就完全失去作用了。
回复 支持 反对

使用道具 举报

发表于 2005-3-7 13:45:05 | 显示全部楼层
怎么那么多new String,
这种字符追加处理,最好用StringBuffer存放
回复 支持 反对

使用道具 举报

发表于 2005-3-7 14:19:12 | 显示全部楼层
对楼上的说的有道理。StringBuffer 用的好,效率是String 的百倍以上。具体我们是有测试的。大概在3个数量级的差距。因为String的+这个操作符也是最后转换成数个StringBuffer的操作。中间new的次数很多,严重影响效率。单用户看不出来,多用户并发环境下相当影响速度
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-7 17:20:10 | 显示全部楼层
Post by popeyelin
你没有考虑时区。这样,到了美国这样的国家多时区,这就完全失去作用了。


时区?

不是吧? 我这是个日历类, 不是时间类.

Post by hantsy
怎么那么多new String,
这种字符追加处理,最好用StringBuffer存放


言之有理

马上修改
回复 支持 反对

使用道具 举报

发表于 2005-3-7 19:03:04 | 显示全部楼层
public String getcalstr(String filename, int mon)
方法的2个参数是干什么用的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-7 20:58:06 | 显示全部楼层
这是我用在页面上的一个bean

指定页面和跳转月数
回复 支持 反对

使用道具 举报

发表于 2005-3-7 21:37:14 | 显示全部楼层
手痒,改了一下你的程序,不知道有没有改坏你的程序,感觉还是有些注释好一些:-)

  1. import java.util.Arrays;
  2. import java.util.Calendar;
  3. import java.util.GregorianCalendar;

  4. public class Calculator {
  5.     String days[];
  6.     StringBuffer strCal;

  7.     /**
  8.      * Creates a new instance of getcal
  9.      */
  10.     public Calculator() {
  11.         days = new String[42];
  12.         Arrays.fill(days, "");
  13.         strCal = new StringBuffer();
  14.     }

  15.     /**
  16.      * @param filename ???
  17.      * @param year  要显示的年份
  18.      * @param month 要显示的月份
  19.      * @return
  20.      */
  21.     public String getCalculatorStr(String filename, int year, int month) {

  22.         //取得当前时间
  23.         GregorianCalendar current = new GregorianCalendar();
  24.         int currDay = current.get(Calendar.DAY_OF_MONTH);
  25.         int currMonth = current.get(Calendar.MONTH);
  26.         int currYear = current.get(Calendar.YEAR);
  27.         strCal.append("当前日期:" + currYear + "年" + (currMonth + 1) + "月" + currDay + "日");

  28.         //设置要显示的月
  29.         Calendar displayMonth = current;
  30.         displayMonth.set(Calendar.YEAR, currYear);
  31.         displayMonth.setFirstDayOfWeek(Calendar.SUNDAY);
  32.         displayMonth.set(Calendar.DAY_OF_MONTH, 1);

  33.         //初始化要显示的月里面的每一天
  34.         int firstIndex = displayMonth.get(Calendar.DAY_OF_WEEK) - 1;
  35.         int maxIndex = displayMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
  36.         for (int i = 0; i < maxIndex; i++) {
  37.             days[firstIndex + i] = String.valueOf(i + 1);
  38.         }

  39.         strCal.append("<table border="0" width="200" height="81">");
  40.         strCal.append("<div align=center>");
  41.         strCal.append("<tr>");
  42.         strCal.append("<th colspan='7'>");
  43.         strCal.append("<a href='" + filename + "?month=" + (month - 1) + "'><<</a> " + month + "月" + " <a href='" + filename + "?month=" + (month + 1) + "'>>></a>");
  44.         strCal.append("</th>");
  45.         strCal.append("</tr>");
  46.         strCal.append("<tr>");
  47.         strCal.append("<th width="25" height="16" ><font color="red">日</font></th>");
  48.         strCal.append("<th width="25" height="16" >一</th>");
  49.         strCal.append("<th width="25" height="16" >二</th>");
  50.         strCal.append("<th width="25" height="16" >三</th>");
  51.         strCal.append("<th width="25" height="16" >四</th>");
  52.         strCal.append("<th width="25" height="16" >五</th>");
  53.         strCal.append("<th width="25" height="16" ><font color="red">六</font></th>");
  54.         strCal.append("</tr>");
  55.         for (int j = 0; j < 6; j++) {
  56.             strCal.append("<tr>");
  57.             for (int i = j * 7; i < (j + 1) * 7; i++) {
  58.                 strCal.append("<td width="15%" height="16"  valign="middle" align="center">");
  59.                 if ((i - firstIndex + 1) == currDay) {
  60.                     strCal.append("<a href=''><font color="red">" + days[i] + "</font></a>");
  61.                 } else {
  62.                     strCal.append("<a href=''>" + days[i] + "</a>");
  63.                 }
  64.                 strCal.append("</td>");
  65.             }
  66.             strCal.append("</tr>");
  67.         }
  68.         strCal.append("</div></table> ");

  69.         return strCal.toString();
  70.     }
  71. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-3-7 23:57:10 | 显示全部楼层
谢 谢!!

回复 支持 反对

使用道具 举报

发表于 2005-3-8 01:10:19 | 显示全部楼层
Post by eTony
时区?

不是吧? 我这是个日历类, 不是时间类.



言之有理

马上修改

如果你在中国开发美国用的程序,差16个小时,你就知道时差的重要性了 。赫赫,我就是是干这个的
差个一天是常事儿。如果不处理时区,那美国人将看到他可以选择未来的日期,嘿嘿。
PS:其实真正的UtcTime只有一个,可能有很多人没有搞明白这个概念
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表