下面的代碼用于顯示當前時間,并根據當前是幾點輸出相應的提示信息,其中,使用java.util.Date thisday=new java.util.Date();語句來取得當前時間,然后通過相應的方法取得年,月,日,時和分的信息并輸出:
<% page contentType="text/html; charset=utf-8" import="java.util.*" %>
<body>
<%
//獲取當前的時間
java.util.Date thisday = new java.util.Date();
%>
當前時間:
<%=thisday.getYear()+1900%>年
<%=thisday.getMonth()+1%>月
<%=thisday.getDate()%>日
<%=thisday.getHours()%>時
<%=thisday.getMinutes()%>分
<%
String str = "歡迎訪問:"
int num = this.getHours();
switch(num){
case 8:
case 9:
case 10:
case 11:
{
out.print("歡迎你的訪問,上午好!");
break;
}
case 12:
{
out.print("中午好!");
break;
}
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
{
out.print("下午好!");
break;
}
</body>