获取jstl中的日期差异
我有一个日期,想要将其与当前时间的差异显示为 --year--month--days--hours--mins--seconds
。我该怎么做这个jstl?可以肯定的是,该日期将大于当前日期时间。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 JSTL,您可以执行一些代码操作,例如:
但正如代码所示,这给出了总体差异,并且几乎不可能是“日历感知”的执行方式。即,您不能说:“自 otherDate 起已过去 3 年零 1 个月零 2 天”。
这种“几天过去了...”样式的另一个示例,使用 JSP 标记并使用“今天/昨天/几天前”演示:
注意:
在您的软件问题域中,如果谈论日历中的日期和月份有意义的话这样,您可能应该在您的域模型中表达这一点。如果没有,至少您应该受益于使用另一个较低的软件层来提供此信息(例如使用 java.util.Calendar 或 Joda-Time API)。
Using JSTL you could do some code gymnastics like:
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:
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).
不确定是否有任何内置方法可以使用 JSTL 执行此操作。您可以编写自己的标签库或可能使用如下所示的表达式语言(EL)。
取自寻找 JSTL Taglib 计算两个日期之间的秒数
Not sure there are any built in ways of doing this with JSTL. you could write your own tag library or potentially use expression language (EL) like below.
taken from Looking for JSTL Taglib calculate seconds between two dates
马克,我不确定使用 JSTL 是否可以实现这一点,一种方法是创建您自己的自定义标记来处理这个问题,如 @olly_uk 建议的那样。我个人不会在 JSP 上使用任何表达式语言,因为这也可能影响可读性并且不是最佳实践。
当构建页面 bean 时,您也可以有这种计算/日期差异,这样可以避免任何 EL 或新标记。这也可能有其局限性,例如我不确定您想要检查差异的日期是否是由用户在您想要立即显示结果的字段中输入的,如果您明白我的意思的话。
另外,您可以根据您的场景尝试使用 jQuery 来计算和显示差异,我想我无论如何都会从 SO 链接此页面。
如何获取号码JavaScript 中两个日期之间的天数?
JQuery 计算日期2 个日期文本框的差异
希望这有帮助。
Mark I'm uncertain if this is possible using JSTL and one way would be to create your own custom tag to handle this as @olly_uk suggested. Me personally would not use any expression language on my JSP as this might also affect readability and not best practise.
You could also have this calculation/date difference when your page bean is constructed, that way avoiding any EL or a new tag. This also might have its limitations such as I'm not sure if the date you want to check the difference is entered by the user on field where you want to display the result instantly etc if you see what I mean.
Also another you could try depending on your scenario is using jQuery to calculate and display the difference, I thought I'll link this page anyway from SO.
How do I get the number of days between two dates in JavaScript?
JQuery Calculate Day Difference in 2 date textboxes
Hope this helps.