37. 第一部分 JSP 技术与 J2EE 技术
}
public int getYear()
{
return calendar.get(Calendar.YEAR);
}
public String getMonth()
{
int m = getMonthInt();
String[] months = new String []
{January February March
April May June
July August September
October November December
};
if (m 12)
return Unknown to Man;
return months[m - 1];
}
public String getDay()
{
int x = getDayOfWeek();
String[] days = new String[]
{Sunday Monday Tuesday Wednesday
Thursday Friday Saturday
};
if (x 7)
return Unknown to Man;
return days[x - 1];
}
public int getMonthInt()
{
return 1 + calendar.get(Calendar.MONTH);
}
public String getDate()
38. 第1章 JavaBeans 组件技术
{
return getMonthInt() + / + getDayOfMonth() + / + getYear();
}
public String getTime()
{
return getHour() + : + getMinute() + : + getSecond();
}
public int getDayOfMonth()
{
return calendar.get(Calendar.DAY_OF_MONTH);
}
public int getDayOfYear()
{
return calendar.get(Calendar.DAY_OF_YEAR);
}
public int getWeekOfYear()
{
return calendar.get(Calendar.WEEK_OF_YEAR);
}
public int getWeekOfMonth()
{
return calendar.get(Calendar.WEEK_OF_MONTH);
}
public int getDayOfWeek()
{
return calendar.get(Calendar.DAY_OF_WEEK);
}
public int getHour()
{
return calendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinute()
{
return calendar.get(Calendar.MINUTE);
39. 第一部分 JSP 技术与 J2EE 技术
}
public int getSecond()
{
return calendar.get(Calendar.SECOND);
}
public int getEra()
{
return calendar.get(Calendar.ERA);
}
public String getUSTimeZone()
{
String[] zones = new String[]
{Hawaii Alaskan Pacific
Mountain Central Eastern
};
int index = 10 + getZoneOffset();
if (index = 5)
{
return zones[10 + getZoneOffset()];
}
else
{
return Only US Time Zones supported;
}
}
public int getZoneOffset()
{
return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000);
}
public int getDSTOffset()
{
return calendar.get(Calendar.DST_OFFSET)/(60*60*1000);
}
public int getAMPM()
40. 第1章 JavaBeans 组件技术
{
return calendar.get(Calendar.AM_PM);
}
}
在程序清单 1.17(JspCalendar.java)中 定义了一个 JavaBean JspCalendar JspCalendar
组件中有很多 getXXX()方法 可以获取各种各样的时间信息 关于这些方法的细节问题
我们在这里就不详细讨论了 这不是本书的主题 对此感兴趣的读者 可以参考相应的 Java
文档
程序清单 1.18
%--
File Name:date.jsp
Author:fancy
Date:2001.4.1
Note:use JspCalendar bean to show the time info.
--%
html
body bgcolor=white
jsp:useBean id='clock' scope='page' class='test.JspCalendar'/
font size=4
ul
liDay of month: is jsp:getProperty name=clock property=dayOfMonth/
liYear: is jsp:getProperty name=clock property=year/
liMonth: is jsp:getProperty name=clock property=month/
liTime: is jsp:getProperty name=clock property=time/
liDate: is jsp:getProperty name=clock property=date/
liDay: is jsp:getProperty name=clock property=day/
liDay Of Year: is jsp:getProperty name=clock property=dayOfYear/
liWeek Of Year: is jsp:getProperty name=clock property=weekOfYear/
liera: is jsp:getProperty name=clock property=era/
liDST Offset: is jsp:getProperty name=clock property=dSTOffset/
liZone Offset: is jsp:getProperty name=clock property=zoneOffset/
/ul
/font
/body
/html
程序清单 1.18(date.jsp)程序十分简单 只是调用 JspCalendar Bean 的各种 getXXX()方
法 输出各种各样的时间信息 读者请注意 JspCalendar Bean 的 Scope 属性是 page 这就
是说 每次刷新当前页面时 该 JavaBean 对象就会被重新创建 重新获取时间信息 所以
每次执行 date.jsp 程序 每次的结果都不一样 程序清单 1.18 的执行结果见图 1.7 图 1.8
41. 第一部分 JSP 技术与 J2EE 技术
图 1.7 date.jsp 程序的运行结果(第一次运行)
图 1.8 date.jsp 程序的运行结果(第二次运行)
由图 1.7 和图 1.8 不难看出 date.jsp 程序前后两次的运行结果显然不同
接下来请看下面的程序清单 1.19(date1.jsp)
程序清单 1.19
%--
File Name:date.jsp
Author:fancy
Date:2001.4.1
Note:use JspCalendar bean to show the time info.
--%
html
body bgcolor=white
jsp:useBean id='clock' scope='application' class='test.JspCalendar'/
42. 第1章 JavaBeans 组件技术
font size=4
ul
liDay of month: is jsp:getProperty name=clock property=dayOfMonth/
liYear: is jsp:getProperty name=clock property=year/
liMonth: is jsp:getProperty name=clock property=month/
liTime: is jsp:getProperty name=clock property=time/
liDate: is jsp:getProperty name=clock property=date/
liDay: is jsp:getProperty name=clock property=day/
liDay Of Year: is jsp:getProperty name=clock property=dayOfYear/
liWeek Of Year: is jsp:getProperty name=clock property=weekOfYear/
liera: is jsp:getProperty name=clock property=era/
liDST Offset: is jsp:getProperty name=clock property=dSTOffset/
liZone Offset: is jsp:getProperty name=clock property=zoneOffset/
/ul
/font
实际时间:%=new java.util.Date()%
/body
/html
程序清单 1.19 和程序清单 1.18 几乎完全相同 只不过在程序清单 1.19 中 JspCalendar
组件的 Scope 属性值为 application 而程序清单 1.19 中 JspCalendar 组件的 Scope 属性值为
page 并且在程序清单 1.19 的最后还使用了另一种方法获取当前的时间 以便对照 那么
程序清单 1.19 的运行效果与程序清单 1.18 相比 究竟有什么不同呢?请看图 1.8 和图 1.9
图 1.8 date1.jsp 程序的运行效果(第一次)