jsp用基础的html显示显示当前日期和月历的功能
时间: 2024-02-26 19:59:13 浏览: 41
要在JSP页面中显示当前日期和月历,可以使用Java的Calendar类和HTML的表格标记来实现。以下是一个简单的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calendar Example</title>
</head>
<body>
<h1>Current Date</h1>
<p><%= java.time.LocalDate.now() %></p>
<h1>Calendar</h1>
<table>
<tr>
<th>Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
<%
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(java.util.Calendar.DAY_OF_MONTH, 1);
int firstDayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK) - 1;
int maxDayOfMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
int dayOfMonth = 1;
while (dayOfMonth <= maxDayOfMonth) {
%>
<tr>
<%
for (int i = 0; i < 7; i++) {
if (i < firstDayOfWeek || dayOfMonth > maxDayOfMonth) {
%>
<td></td>
<%
} else {
%>
<td><%= dayOfMonth %></td>
<%
dayOfMonth++;
}
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>
```
这个JSP页面中使用了Java的LocalDate类来获取当前日期,并使用HTML标记来显示。同时,使用Java的Calendar类来计算月历中的日期,并通过JSP标记和HTML表格来动态生成月历。其中,`java.util.Calendar.getInstance()`方法获取当前日期的Calendar对象,`cal.set(java.util.Calendar.DAY_OF_MONTH, 1)`方法将当前日期设置为当月的第一天,`cal.get(java.util.Calendar.DAY_OF_WEEK)`方法获取当前日期是星期几,`cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH)`方法获取当前月份的最大天数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)