tags:

views:

5205

answers:

5

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.

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?

+1  A: 

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

// Date d given
d.setTime(d.getTime()+86400000);
sirprize
This is not a good idea. In locales that use Daylight Savings Time, this will give the wrong day a couple days a year, depending on the time, and it will drive you nuts trying to figure out what's wrong.
Frank Pape
+2  A: 

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

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

Truly horrible.

jodonnell
+1  A: 

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

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.

mbaird
+6  A: 

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

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

<?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&lt;/uri&gt;
  <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>
ScArcher2
A: 

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.

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

Mwanji Ezana