如何在 JSP 中打印当前日期? [英] How to print current date in JSP?
问题描述
我想做这样的事情:
<?php echo date('Y'); ?>
但是在 .jsp
文件中.我看到的所有教程都需要在某处构建一个类.我们正在运行 appFuse 和 Tapestry.当然,其中之一(如果不是 Java 本身)为我们提供了一些可以在没有所有开销的情况下完成此类事情的方法.
But then in a .jsp
file. All the tutorials I'm seeing require building a class somewhere. We're running appFuse and Tapestry. Surely one of those (if not Java itself) provide us with something to do this sort of thing without all that overhead.
这看起来应该有效,但没有:
This seems like it should work, but doesn't:
<%= new Date.getYear() %>
推荐答案
使用 jsp:useBean
构造一个 java.util.Date
实例并使用 JSTL fmt:formatDate
使用 SimpleDateFormat
模式.
Use jsp:useBean
to construct a java.util.Date
instance and use JSTL fmt:formatDate
to format it into a human readable string using a SimpleDateFormat
pattern.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" />
Current year is: <fmt:formatDate value="${date}" pattern="yyyy" />
老式的 scriptlet 方式是:
<%= new java.text.SimpleDateFormat("yyyy").format(new java.util.Date()) %>
请注意,当您不使用 @page import
指令时,您需要指定完全限定的类名,这可能是导致问题的原因.然而,使用 scriptlets 非常不鼓励,因为十年.
Note that you need to specify the full qualified class name when you don't use @page import
directives, that was likely the cause of your problem. Using scriptlets is however highly discouraged since a decade.
这一切都在 [jsp]
标签信息页面 中展示了 :)
This all is demonstrated in the [jsp]
tag info page as well :)
这篇关于如何在 JSP 中打印当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!