在JSP程序中使用各种脚本元素和标签实现具体的功能
<%--
Created by IntelliJ IDEA.
User: asus
Date: 2020/2/25
Time: 21:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*"%>
<html>
<head>
<title>本月日历</title>
</head>
<%!String days[];%>
<body>
<%days=new String[42]; %><!--6*7的表格-->
<%
for(int i=0;i<42;i++){
days[i]="";
}
%>
<%
GregorianCalendar currentday=new GregorianCalendar();
int year=currentday.get(Calendar.YEAR);
int month=currentday.get(Calendar.MONTH);
int day=currentday.get(Calendar.DAY_OF_MONTH);
out.println(year+"年"+ (month+1)+"月"+day+"日");
Calendar currentmonth=Calendar.getInstance();
currentmonth.set(Calendar.DAY_OF_YEAR,year);
currentmonth.set(Calendar.DAY_OF_MONTH,month);
currentmonth.setFirstDayOfWeek(Calendar.SUNDAY);//设置每周第一天是周日
currentmonth.set(Calendar.DAY_OF_MONTH,1);//设置每月第一天是一号
int first=currentmonth.get(Calendar.DAY_OF_WEEK)-1;//本月第一天是周几,也就是当月日历显示的第一个数字
int last=currentmonth.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int i=0;i<last;i++){
days[first+i]=String.valueOf(i+1);
}
%>
<table border="0" width="161" height="100">
<div align=center>
<tr>
<th bgcolor="yellow" width="25" height="16"><font color="red">日</font></th>
<th bgcolor="yellow" width="25" height="16">一</th>
<th bgcolor="yellow" width="25" height="16">二</th>
<th bgcolor="yellow" width="25" height="16">三</th>
<th bgcolor="yellow" width="25" height="16">四</th>
<th bgcolor="yellow" width="25" height="16">五</th>
<th bgcolor="yellow" width="25" height="16"><font color="green">六</font> </th>
</tr>
<%for(int i=0;i<6;i++){%>
<tr>
<% for(int j=i*7;j<(i+1)*7;j++){%>
<%if((j-first+1)==day){%><!--当天-->
<td bgcolor="red"><%=days[j]%></td>
<%}else{%>
<td bgcolor="#808080"> <%=days[j]%></td>
<%}%>
<%}%>
<% }%>
</tr>
</div>
</table>
</body>
</html>
Main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main</title>
</head>
<body>
<jsp:include page="rili.jsp"></jsp:include>
</body>
</html>
相关知识:
1.关于GregorianCalendar公历类:
GregorianCalendar是Calendar的实现类,提供了世界上大多数国家使用的标准日历系统。它的主要方法有:GregorianCalendar.get(GregorianCalendar.YEAR); //得到年GregorianCalendar.get(GregorianCalendar.MONTH); //得到月GregorianCalendar.get(GregorianCalendar.DATE); //得到天GregorianCalendar.get(GregorianCalendar.Day_OF_WEEK); //一星期的第几天GregorianCalendar.get(GregorianCalendar.Day_OF_MONTH); //一月的第几天
在GregorianCalendar里面,星期天是一周开始的第一天;0代表现实生活中的一月,11代表十二月。
2.关于Calendar日历类:
静态方法getInstance()
Calendar类的静态方法getInstance()可以初始化一个日历对象:Calendar now = Calendar.getInstance();
set()方法:使用下面三个方法把日历定到任何一个时间:
set(int year ,int month,int date)
set(int year ,int month,int date,int hour,int minute)
set(int year ,int month,int date,int hour,int minute,int second)
获取年份、月份、小时等信息可以使用方法:
get(Calendar.Month);这样的方法 0表示一月,1表示二月
get(Calendar.DAY_OF_MONTH)获得这个月的第几天
get(Calendar.DAY_OF_WEEK)获得这个星期的第几天
get(Calendar.DAY_OF_YEAR)获得这个年的第几天
getTimeMillis()获得当前时间的毫秒表示