在JSTL / JSP中,给定java.util.Date,我如何找到第二天? [英] In JSTL/JSP, given a java.util.Date, how do I find the next day?

查看:70
本文介绍了在JSTL / JSP中,给定java.util.Date,我如何找到第二天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On a JSTL/JSP page, I have a java.util.Date object from my application. I need to find the day after the 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.

有没有办法使用JSP或JSTL用标签实现这一目标而不必切换到全面的Java,或者后者是实现这一目标的唯一方法吗?

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?

推荐答案

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

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

我使用静态方法和taglib来实现这一点。

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

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

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

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

这篇关于在JSTL / JSP中,给定java.util.Date,我如何找到第二天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆