jsp用年与日展示时间
时间: 2024-05-08 16:17:10 浏览: 16
以下是使用JSP展示当前时间的示例代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示当前时间</title>
</head>
<body>
<h1>当前时间: <%=java.time.LocalDate.now().getYear()%>年 <%=java.time.LocalDate.now().getDayOfYear()%>日</h1>
</body>
</html>
```
在这个示例中,我们使用了Java 8中的LocalDate类来获取当前时间的年份和日数,并将它们展示在页面上。注意到当我们在JSP页面中使用Java代码时,我们需要将代码放在`<% %>`标记中。
如果你想要更加精细地控制时间的输出格式,你可以使用Java 8中的DateTimeFormatter类。以下是一个示例代码:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.time.LocalDate"%>
<%@ page import="java.time.format.DateTimeFormatter"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>展示当前时间</title>
</head>
<body>
<%
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年 D日");
String formattedDate = now.format(formatter);
%>
<h1>当前时间: <%=formattedDate%></h1>
</body>
</html>
```
在这个示例中,我们首先导入了LocalDate和DateTimeFormatter类。然后我们使用LocalDate.now()方法获取当前时间,使用DateTimeFormatter.ofPattern()方法创建一个自定义的日期格式,再使用LocalDate.format()方法将日期格式化为字符串。最后,我们将格式化后的字符串展示在页面上。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)