I have a date and want to show difference of it with current time as --year--month--days--hours--minutes--seconds
. How can I do this jstl? It is sure that the date will be greater than current datetime.
Using JSTL you could do some code gymnastics like:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:parseNumber
value="${(now.time - otherDate.time) / (1000*60*60*24) }"
integerOnly="true" /> day(s) passed between given dates.
But as code suggests, this gives overall difference and hardly could be a "calendar aware" way of doing it. I.e. You could not say: "3 years, 1 month and 2 days passed since otherDate".
Another example for this "days passed..." style, using a JSP tag and using "today/yesterday/days back" presentation:
<%--[...]--%>
<%@attribute name="otherDate" required="true" type="java.util.Date"%>
<jsp:useBean id="now" class="java.util.Date" scope="request"/>
<fmt:parseNumber
value="${ now.time / (1000*60*60*24) }"
integerOnly="true" var="nowDays" scope="request"/>
<fmt:parseNumber
value="${ otherDate.time / (1000*60*60*24) }"
integerOnly="true" var="otherDays" scope="page"/>
<c:set value="${nowDays - otherDays}" var="dateDiff"/>
<c:choose>
<c:when test="${dateDiff eq 0}">today</c:when>
<c:when test="${dateDiff eq 1}">yesterday</c:when>
<c:otherwise>${dateDiff} day(s) ago</c:otherwise>
<%--[...]--%>
Note:
In your software problem domain, if it makes sense to talk about days and months in a calendar way, probably you should have that expressed in your Domain Model. If not, at least you should benefit from using another lower software layer to provide this information (and for example using java.util.Calendar or Joda-Time APIs).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With