Get date differences in jstl

I have a date and want to show the difference with the current time as --year--month--days--hours--minutes--seconds . How can i do this jstl? I am sure that the date will be more than the current date and time.

+4
source share
3 answers

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).

+2
source

Not sure if there are built-in ways in JSTL to do this. you can write your own tag library or perhaps use an expression language (EL) as shown below.

 ${(dateObj) - (now.time)} 

taken from JSTL Search Taglib calculates seconds between two dates

0
source

Mark. I'm not sure if this is possible using JSTL, and one way is to create your own tag for processing, as @olly_uk suggested. I personally will not use the expression language on my JSP, as this may also affect readability, not best practice.

You can also have this difference in the calculation / date when your bean page is built in such a way as to avoid using an EL or a new tag. It may also have its limitations, such as I'm not sure that the date you want to check the difference is entered by the user in the field where you want to instantly display the result, etc., if you understand what I mean.

You can also try using jQuery to calculate and display the difference depending on your scenario, I thought that I would link this page to SO anyway.

How to get the number of days between two dates in JavaScript?

JQuery Calculate daily difference in two-date text files

Hope this helps.

0
source

Source: https://habr.com/ru/post/1393118/


All Articles