java 在简单的 JSP 程序中显示日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29478673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying date in simple JSP program
提问by sliziky
I've copied the following JSP code from another page but when I view it from a browser, the date does not display.
我从另一个页面复制了以下 JSP 代码,但是当我从浏览器查看它时,日期没有显示。
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
I've saved it as file.jsp
.
我已将其保存为file.jsp
.
采纳答案by headlikearock
You need to add the following as the first line:
您需要添加以下内容作为第一行:
<%@page contentType="text/html" import="java.util.*" %>
A tutorial can be found here: http://www.java-samples.com/showtutorial.php?tutorialid=81
可以在此处找到教程:http: //www.java-samples.com/showtutorial.php?tutorialid =81
回答by Rajesh N
Try this (file.jsp):
试试这个(file.jsp):
<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<html>
<head>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
%>
</head>
<body>
<p>Hello! The time is now <%=date%></p>
</body>
</html>
You can change the date format that suits you in "yyyy-MM-dd HH:mm:ss"
. And, <%=date%>
displays the result. Good luck mate :)
您可以更改适合您的日期格式"yyyy-MM-dd HH:mm:ss"
。并且,<%=date%>
显示结果。祝你好运:)
Result:
结果:
Hello! The time is now 2015-04-07 11:25:47
Note:You have to save this file and run it from a webserver like Tomcat.
注意:您必须保存此文件并从 Tomcat 等网络服务器运行它。