在 JSTL/JSP 中,给定一个 java.util.Date,我如何找到第二天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/74248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:57:02  来源:igfitidea点击:

In JSTL/JSP, given a java.util.Date, how do I find the next day?

javajspjstl

提问by Sean McMains

On a JSTL/JSP page, I have a java.util.Date object from my application. I need to find the day afterthe day specified by that object. I can use <jsp:scriptlet> to drop into Java and use java.util.Calendar to do the necessary calculations, but this feels clumsy and inelegant to me.

在 JSTL/JSP 页面上,我的应用程序中有一个 java.util.Date 对象。我需要找了一天后,该对象指定的日期。我可以使用 <jsp:scriptlet> 进入 Java 并使用 java.util.Calendar 进行必要的计算,但这对我来说感觉笨拙和不雅。

Is there some way to use JSP or JSTL tags to achieve this end without having to switch into full-on Java, or is the latter the only way to accomplish this?

是否有某种方法可以使用 JSP 或 JSTL 标签来实现这一目标,而不必切换到完整的 Java,或者后者是实现这一目标的唯一方法?

回答by ScArcher2

I'm not a fan of putting java code in your jsp.

我不喜欢将 java 代码放入您的 jsp 中。

I'd use a static method and a taglib to accomplish this.

我会使用一个静态方法和一个 taglib 来完成这个。

Just my idea though. There are many ways to solve this problem.

只是我的想法。有很多方法可以解决这个问题。

public static Date addDay(Date date){
   //TODO you may want to check for a null date and handle it.
   Calendar cal = Calendar.getInstance();
   cal.setTime (date);
   cal.add (Calendar.DATE, 1);
   return cal.getTime();
}

functions.tld

函数.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">
  <description>functions library</description>
  <display-name>functions</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>xfn</short-name>
  <uri>http://yourdomain/functions.tld</uri>
  <function>
    <description>
      Adds 1 day to a date.
    </description>
    <name>addDay</name>
    <function-class>Functions</function-class>
    <function-signature>java.util.Date addDay(java.util.Date)</function-signature>
    <example>
      ${xfn:addDay(date)}
    </example>
  </function>
</taglib>

回答by sirprize

While this does not answer your initial question, you could perhaps eliminate the hassle of going through java.util.Calendar by doing this:

虽然这不能回答您最初的问题,但您可以通过执行以下操作来消除通过 java.util.Calendar 的麻烦:

// Date d given
d.setTime(d.getTime()+86400000);

回答by jodonnell

You have to either use a scriptlet or write your own tag. For the record, using Calendar would look like this:

您必须使用 scriptlet 或编写自己的标记。为了记录,使用日历看起来像这样:

Calendar cal = Calendar.getInstance();
cal.setTime (date);
cal.add (Calendar.DATE, 1);
date = cal.getTime ();

Truly horrible.

真的很可怕。

回答by Mark B

Unfortunately there is no tag in the standard JSP/JSTL libraries that I know of that would allow you to do this date calculation.

不幸的是,我所知道的标准 JSP/JSTL 库中没有允许您进行日期计算的标记。

The simplest, and most inelegant, solution is to just use some scriptlet code to do the calculation. You've already stated that you think this is a clunky solution, and I agree with you. I would probably write a custom JSP taglib to get this if I were you.

最简单、最不雅的解决方案是仅使用一些 scriptlet 代码来进行计算。你已经说过你认为这是一个笨拙的解决方案,我同意你的看法。如果我是你,我可能会编写一个自定义的 JSP taglib 来获得它。

回答by Mwanji Ezana

In general, I think JSPs should not have data logic. They should get all the data they need to display from the Controller and all their logic should be about HOW the data is displayed, not WHAT is displayed. This is usually a lot simpler and a lot less code/XML than adding a custom tag.

一般来说,我认为 JSP 不应该有数据逻辑。他们应该从控制器获取他们需要显示的所有数据,并且他们所有的逻辑都应该是关于数据如何显示,而不是显示什么。这通常比添加自定义标签简单得多,代码/XML 也少得多。

And if there isn't any re-use happening, is a tiny scriptlet really that much worse than the taglib XML?

如果没有任何重用发生,那么一个小小的 scriptlet 真的比 taglib XML 差那么多吗?