如何在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 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
指令时,需要指定完整的合格类名,这很可能是造成问题的原因.但是不推荐使用十年.
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屋!