在JSP中获取今天的日期并将其与Date变量进行比较

在JSP中获取今天的日期并将其与Date变量进行比较

本文介绍了在JSP中获取今天的日期并将其与Date变量进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面显示了今天抓取的日期/时间,然后再显示在IF语句中进行比较。我确定我硬编码的格式是错误的,但输出的格式是默认的格式。所以问题是如何查看一个日期是否低于另一个日期?如何才能将日期格式化为日期?

  < p> 
< c:set var =testmevalue =<%= new java.util.Date()%>/>
< b>< c:out value =$ {testme}/>< / b>
< / p>

< c:if test =$ {testme lt'Tue Jan 29 16:02:58 GMT 2013'}>
< p>< b>< span class =wrap>测试日期较少。< / span>< / b>< / p>
< / c:if>


解决方案

对于初学者,不要混合 和taglibs / EL。这只是麻烦的食谱。使用一个或另一个,而不是两个。假设你想继续使用taglibs / EL,那么你可以这样做:

 < p> 
< jsp:useBean id =todayclass =java.util.Date/>
< b>< c:out value =$ {today}/>< / b>
< / p>

< fmt:setLocale value =en_US/>
< fmt:parseDate var =testdatevalue =Tue Jan 29 16:02:58 GMT 2013​​pattern =EEE MMM dd HH:mm:ss z yyyy/>
< c:if test =$ {today.time gt testdate.time}>
< p>< b>< span class =wrap>测试日期小于现在。< / span>< / b>< / p>
< / c:if>

请注意,我还在变量名称和描述中修复了一些奇怪的对比逻辑。


I am trying to compare dates, on a basic example and cannot work out the best way of doing it.

The below shows grabbing todays date/time and then comparing it in an IF statement. I am sure the format I have hard coded in is wrong, but when outputing the format that is the format it defaults to. So the question is how do I see if one date is lower than the other and how do I format them to date only?

<p>
    <c:set var="testme" value="<%=new java.util.Date()%>"/>
    <b><c:out value="${testme}"/></b>
</p>

<c:if test="${testme lt 'Tue Jan 29 16:02:58 GMT 2013'}">
    <p><b><span class="wrap">Test date is less.</span></b></p>
</c:if>
解决方案

For starters, don't mix Scriptlets and taglibs/EL. That's just recipe for trouble. Use the one or the other and not both. Assuming that you want to go ahead with taglibs/EL, here's how you could do it:

<p>
    <jsp:useBean id="today" class="java.util.Date" />
    <b><c:out value="${today}"/></b>
</p>

<fmt:setLocale value="en_US" />
<fmt:parseDate var="testdate" value="Tue Jan 29 16:02:58 GMT 2013" pattern="EEE MMM dd HH:mm:ss z yyyy" />
<c:if test="${today.time gt testdate.time}">
    <p><b><span class="wrap">Test date is less than now.</span></b></p>
</c:if>

Note that I also fixed some strange contra-logic in variable names and descriptions.

这篇关于在JSP中获取今天的日期并将其与Date变量进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:09