Get today's date in JSP and compare it to a Date variable
Asked Answered
G

3

9

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>
Gebhardt answered 29/1, 2013 at 16:4 Comment(0)
T
14

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.

Tigrinya answered 29/1, 2013 at 16:15 Comment(3)
Hi some great advice on scriplets and taglibs! Literally dropping that into a JSP, the text in the "if" statement does not show. I see you used testdate.time, does this method write the dates as time only or date/time?Gebhardt
Perhaps you forgot to import the fmt taglib. As to the .time, it just calls the time getter the usual EL way as in date.getTime() which returns a long value, valid for comparison.Tigrinya
Oh dear, no import :( Ok that's looking good and I like the fact its converted to time to make it better.Gebhardt
S
2

Code for compare two dates in JSTL. First format the two dates then compare using

gt-greaterthan

lt-lessthan

ge-greaterthan equal to

le-lessthan equal to

<fmt:formatDate var="date_to_comare" value="${date_to_comare_value}" pattern="yyyy-MM-dd HH:mm:ss"/>
<c:set var="today_date" value="<%=new java.util.Date()%>"/>
<fmt:formatDate var="today_formated_date" value="${today}" pattern="yyyy-MM-dd HH:mm:ss"/>  
<c:if test="${date_to_comare lt today_formated_date}">
Write your code here
</c:if>
Scarlettscarp answered 26/2, 2018 at 11:41 Comment(0)
X
1

You can do something like this:

<%@ taglib  uri = "<pathToLibraries>/fmt.tld" prefix = "fmt" %>

<%------  Do Something between dates ----------%>
<jsp:useBean id="now" class="java.util.Date"/>

<c:set var = "start" value = "30-07-2019" />
<c:set var = "end" value = "24-09-2019" />

<fmt:parseDate value = "${start}" var = "parsedStartDate" pattern = "dd-MM-yyyy" />
<fmt:parseDate value = "${end}" var = "parsedEndDate" pattern = "dd-MM-yyyy" />

<c:if test="${(parsedStartDate le now) && (parsedEndDate ge now)}">
  <%------  Do Something between dates ----------%>
</c:if>

And the different options ge, le, gt, lt etc are already discussed.

Xanthochroid answered 12/7, 2019 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.