如何使用JSTL在JSP中的变量中捕获当前日期和时间?

问题描述:

如何使用JSTL在JSP中捕获变量中的当前日期和时间?

How to capture current date and time in a variable in JSP using JSTL?

<c:set property="currentTime" value="${System.CurrentTimeMillis()}"/> 

我在JSP中使用上面的代码,但是抛出了错误.

I am using the above code in my JSP But it has thrown an error.

org.apache.jasper.JasperException: The function currentTimeMillis must be used with a prefix when default namespace is not specified.

balu,尝试这个..

balu, try this one..

<jsp:useBean id="now" class="java.util.Date" />

<fmt:formatDate type="time" value="${now}" /><br/>
<fmt:formatDate type="date" value="${now}" /><br/>
<fmt:formatDate type="both" value="${now}" /><br/>
<fmt:formatDate type="both" dateStyle="short" timeStyle="short"
    value="${now}" /><br/>
<fmt:formatDate type="both" dateStyle="medium" timeStyle="medium"
    value="${now}" /><br/>
<fmt:formatDate type="both" dateStyle="long" timeStyle="long"
    value="${now}" /><br/>
<fmt:formatDate pattern="yyyy-MM-dd" value="${now}" /><br/>

更新的代码.

<jsp:useBean id="now" class="java.util.Date" />
<jsp:useBean id="bean" class="demo.Sample" />
<c:if test="${bean.timestamp lt now}">
Start date time is in the past.
</c:if>
<c:if test="${bean.timestamp gt now}">
Start date time is in the future.
</c:if>
<c:if test="${bean.timestamp eq now}">
Start date time is identical to now.
</c:if>

balu试试这个..

balu try this one..