在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 类,写这个不是很难,呵呵,下一步要做的是要向日历里添加活动信息.
每遇到一个没有写过的技术问题,都觉得很困难,想放弃,可是我不能!我能做的只是坚持,还有培养自己的兴趣,
当你写过这个程序,就觉得,如此而已啊,哈哈……
自己每天都进步一些,这种感觉,真好!
