编程语言

img yq76034150

一个关于日历的JSP程序!

发表于2004/9/25 16:11:00  1052人阅读

一个JSP+JAVABEANS实现的WEB日历程序,主要使用了Calendar类!
monthBean.java
package test;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.util.*;

public class monthBean {
public final static int[]DAYSINMONTH={31,28,31,30,31,30,31,31,30,31,30,31};
public String []monthNames=null;
protected DateFormatSymbols symbols=null;
protected Calendar cal=Calendar.getInstance(); 
protected int today=cal.get(Calendar.DAY_OF_MONTH); 
protected int month=cal.get(Calendar.MONTH);
protected int year=cal.get(Calendar.YEAR);

public monthBean() {   
  symbols=new DateFormatSymbols();   
  monthNames=symbols.getMonths(); 
  }
public void setMonth(int monthArg){   
   this.month=monthArg; 
   }
public void setYear(int yearArg) {
   this.year=yearArg;
   }
public void setMonth(String monthArg) {   
   try{   
      int month=Integer.parseInt(monthArg);     
      setMonth(month);  
     }catch(Exception exc){
     }
   }
public void setYear(String yearArg){     
   try{    
       int year=Integer.parseInt(yearArg);
       setYear(year); }catch(Exception exc){
       } 
     }
public String getMonthName() { 
      return monthNames[cal.get(Calendar.MONTH)];
      }
public int getToday(){
       return today;
       }
public int getStartCell() {
     Calendar beginOfMonth=Calendar.getInstance();    
     beginOfMonth.set(year,month,0);   
     return beginOfMonth.get(Calendar.DAY_OF_WEEK);
     } 
public int getEndCell() {
     cal.set(year,month,1); int endCell=DAYSINMONTH[month]+getStartCell()-1;   
     if(month==Calendar.FEBRUARY && ((GregorianCalendar)cal).isLeapYear(year)){      
          endCell++;  
           } 
     return endCell;
    } 
public void update()  { 
     cal.set(this.year,this.month,1);
    } 
public String getDayName(int day,boolean longFormat) {
    if(longFormat)  
       return symbols.getWeekdays()[day];    
    return symbols.getShortWeekdays()[day];
    }
  }

month.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="test.monthBean"%>
<jsp:useBean id="cal"  class="test.monthBean" scope="request"/>
<%
   int today=cal.getToday();
%>
<html>
<head>
<title>
日历
</title>
</head>
<body bgcolor="silver" ><br><br><br>
<table border="1" align="center" width="40%" height="100"><tr bgcolor="silver" >
<td colspan="7">&nbsp;<font size="+2">
<%=cal.getMonthName()%>
</font></td></tr>
<tr bgcolor="black" align="center">
<% for(int i=1;i<8;i++) {
   out.print("<td width=/"70/">");
   out.print("<font size/"-1/" color=/"white/">");
   out.print(cal.getDayName(i,true));
out.print("</front> </td>");
}  %>
</tr>
<% int startcell=cal.getStartCell();
   int endcell=cal.getEndCell();
    for(int cellNo=0,day=1;cellNo<42;cellNo++) {
          if(cellNo%7==0) { out.print("<tr>"); }
    out.print("<td valign=top height=57");
           if(cellNo<startcell||cellNo>endcell) {
               out.print(" bgcolor=/"gray/">"); }
 else { 
              if(day==today){  //如果为当前日期,背景色设为绿色
                   out.print(" bgcolor=/"green/">");
                        }
              else
                   out.print(">");
%>
<%=day%>
 <% 
     day++;
 }
    out.println("</td>");
            if(cellNo+1%7==0) {
                 out.print("</tr>"); }
    }   //end for loop
 %>

   </table>

</body>
</html>

  效果如下图:


 

阅读全文
0 0

相关文章推荐

img
取 消
img