在core Java 那本书里,在讲类之前就将了一个打印一个月份中每天是星期几的万年历程序.最近写程序的时候终于要用到了.我花费了半天时间终于弄明白了GregorianCalendar类,并且通过网络抓取oracle数据的时间,然后显示在JSP 网页上.
运行效果如下:
首先要在J2se里面写出万年历程序,作为练习:
1)星期日Calendar.SUNDAY =1 星期日为1,那么星期几就+1为以此类推!
打印月历的程序:
package moonsoft.j2se.testYueli;
import java.util.*;
public class TestYueli
{
public static void main(String [] args)
{ //建立一个GregorianCalendar 对象
GregorianCalendar d=new GregorianCalendar();
//获得系统默认的日期的月份和日期所在的月第几天
int month=d.get(Calendar.MONTH);
int day=d.get(Calendar.DAY_OF_MONTH);
//把d设置为当月第一天
d.set(Calendar.DAY_OF_MONTH, 1);
//获得第一天的是一星期的第几天
int weekday=d.get(Calendar.DAY_OF_WEEK);
System.out.println("SUN MON TUE WED THU FRI SAT");
for(int i=Calendar.SUNDAY; i<weekday; i++)
System.out.print(" ");
//System.out.print(monthday);
do
{
int days=d.get(Calendar.DAY_OF_MONTH);
if(days<10)
System.out.print(" ");
System.out.print(days);
if(days==day)
System.out.print("*");
else
System.out.print(" ");
if(weekday==Calendar.SATURDAY)
System.out.println();
d.add(Calendar.DAY_OF_MONTH, 1);
weekday=d.get(Calendar.DAY_OF_WEEK);
}while(d.get(Calendar.MONTH)==month);
} }
2)注意GregorianCalendar calendar=new(year, month-1,year);
month-1! 在Calendar 0代表一月!!!
然后再写oracle抓取到JSP 页面的程序:
<%@ page contentType="text/html;charset=Big5"%><%@ page import="java.util.*"%><%@ page import="java.sql.*"%><%@ page import="javax.sql.*"%><%@ page import="javax.naming.*"%><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Big5"> <title>社團活動</title> <style type="text/css"><!--.style1 {color: #FFFFFF}--> </style> </head> <% request.setCharacterEncoding("big5"); String name=(String)session.getAttribute("name"); String queryDate=" select to_char(systimestamp, 'YYYY-MM-DD HH24:MM:SS') from dual "; int day=1; int month=3; int year=2005; Connection conn=null; try{ Context ctx=new InitialContext(); DataSource ds=(DataSource)ctx.lookup("jdbc/asdbCoreDS"); conn=ds.getConnection(); PreparedStatement ps=conn.prepareStatement(queryDate); ResultSet rs=ps.executeQuery(); while(rs.next()){ String date=rs.getString(1); year=Integer.parseInt(date.substring(0,4)); month=Integer.parseInt(date.substring(5,7)); day=Integer.parseInt(date.substring(8,10)); out.println(year+"年"+month+"月"); } } catch(Exception e1){e1.printStackTrace();} finally{conn.close();} %> <body> <table width="100%" border="1" cellspacing="1" cellpadding="1"> <tr bgcolor="#0099FF"> <th scope="col"><span class="style1">SUN</span></th> <th scope="col"><span class="style1">MON</span></th> <th scope="col"><span class="style1">TUE</span></th> <th scope="col"><span class="style1">WED</span></th> <th scope="col"><span class="style1">THU</span></th> <th scope="col"><span class="style1">FRI</span></th> <th scope="col"><span class="style1">SAT</span></th> </tr> <tr> <% GregorianCalendar d=new GregorianCalendar(year, month-1 , day); int monthDay=d.get(Calendar.DAY_OF_MONTH); int monthNumber=d.get(Calendar.MONTH); d.set(Calendar.DAY_OF_MONTH, 1); int weekDay=d.get(Calendar.DAY_OF_WEEK); for(int i=Calendar.SUNDAY; i<weekDay; i++) {%><td height="45"> </td><%} do{ int monthDays=d.get(Calendar.DAY_OF_MONTH); %><td height="45"><%=monthDays%></td><% if(weekDay==Calendar.SATURDAY) out.println("</tr><tr>"); d.add(Calendar.DAY_OF_MONTH, 1); weekDay=d.get(Calendar.DAY_OF_WEEK); }while(d.get(Calendar.MONTH)==monthNumber); %> </table> </body></html>
其实如果掌握了GregorianCalendar 类,写这个不是很难,呵呵,下一步要做的是要向日历里添加活动信息.
每遇到一个没有写过的技术问题,都觉得很困难,想放弃,可是我不能!我能做的只是坚持,还有培养自己的兴趣,
当你写过这个程序,就觉得,如此而已啊,哈哈......
自己每天都进步一些,这种感觉,真好!