一個關於日曆的JSP程式!__JSP

來源:互聯網
上載者:User

一個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>

  效果如下圖:


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.