jsp页面获取系统时间代码
时间: 2024-07-16 15:00:44 浏览: 72
在JavaServer Pages (JSP) 中,获取系统当前时间通常使用Java的内置类`java.util.Date`或其包装类`java.time.LocalDateTime`。以下是一个简单的例子:
1. 使用`java.util.Date`:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%
Date currentTime = new Date();
out.println("当前系统时间为: " + currentTime);
%>
</body>
</html>
```
这将显示当前的日期和时间。
2. 使用`java.time.LocalDateTime` (Java 8及以上):
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%
LocalDateTime currentDateTime = LocalDateTime.now();
String formattedDateTime = currentDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
out.println("当前系统时间为: " + formattedDateTime);
%>
</body>
</html>
```
这里使用了`LocalDateTime.now()`获取当前日期和时间,并用`DateTimeFormatter`格式化输出。
阅读全文
相关推荐

















