Using JSTL, you can do some program gymnastics, for example:
<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 the code suggests, this gives a general difference and can hardly be a way to "calendar awareness". That is, you could not say: "3 years, 1 month and 2 days have passed from another date."
Another example for this “days have passed ...” style using the JSP tag and using the “today / yesterday / days ago” 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 problematic software domain, if it makes sense to talk about days and months in calendar mode, perhaps you should have this expressed in the Domain Model . If not, at least you should use another lower level software to provide this information (and, for example, using java.util.Calendar or Joda-Time APIs).
source share