Introduction

Joda-Time JSP tags provide a JSTL-like set of tags to format and parse Joda-Time based date and time objects.

The JSTL fmt tags work with java.util.Date and java.util.TimeZone objects. The tags in this library work with Joda-Time DateTime, ReadableInstant, ReadablePartial, and DateTimeZone objects. Otherwise, the two libraries are very similar, thus you can refer to standard JSTL books and documentation to supplement this document.

Setup

You will need a servlet container which supports Servlet 2.4, JSP 2.0, JSTL 1.1. One such container is Tomcat 5.

The simplest setup is to copy the joda-time and joda-time-jsptags jar files to the WEB-INF/lib directory of your web application.

You then need to declare the library as follows at the top of your jsp pages:

<%@taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>

In addition to these steps you should ensure that your application is using servlet specification 2.4. This is usually controlled by the web.xml file. If you do not do this then EL expressions will not work as expected.

Format tag

This tag formats ReadableInstant (including DateTime) and ReadablePartial (including LocalDateTime, LocalDate and LocalTime) objects. The time zone may be specified using an attribute, an enclosing <joda:dateTimeZone/> tag, preceding <joda:timeZone/> tag, or via the "org.joda.time.dateTimeZone" scoped variable.

The tag may be used in one of two ways. The simplest way is to output directly to the HTTP response. The second way is to set the var and scope attributes and set the value of a variable.

Example:

  <% pageContext.setAttribute("now", new org.joda.time.DateTime()); %>
  <joda:format value="${now}" style="SM" />

Attributes
value (required) Must be a ReadableInstant or ReadablePartial
var The scoped variable to set
scope The scope of the variable to set
locale The locale to use for formatting
style The style to use for formatting (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None)
pattern The pattern to use for formatting (see Joda format documentation for recognized pattern strings)
dateTimeZone The time zone to use for formatting

ParseDateTime tag

This tag parses a String into a DateTime object. The time zone may be specified using an attribute, an enclosing <joda:dateTimeZone/> tag, preceding <joda:timeZone/> tag, or via the "org.joda.time.dateTimeZone" scoped variable.

The tag may be used in one of two ways. The simplest way is to parse the input and then directly format the output to the HTTP response. The second way, and more usual way, is to set the var and scope attributes and set the value of a variable with the results of the parse.

Example:

  <joda:parseDateTime var="parsed" pattern="yy/M/d" value="05/11/19" />
  <joda:format value="${parsed}" style="L-" />

Attributes
value (required; unless value is nested within tag) Must be a string which can be parsed into a DateTime according to the parsing options specified
var The scoped variable to set
scope The scope of the variable to set
locale The locale to use for parsing
style The style to use for parsing (two characters, one for date, one for time, from S=Short, M=Medium, L=Long, F=Full, -=None)
pattern The pattern to use for parsing (see Joda format documentation for recognized pattern strings)
dateTimeZone The time zone to use for parsing

DateTimeZone tag

This tag sets the default time zone to use for all nested tags. The <joda:format /> tag may override this value with an explicit dateTimeZone attribute.

Example:

  <% pageContext.setAttribute("now", new DateTime()); %>
  <% pageContext.setAttribute("bkk", DateTimeZone.forID("Asia/Bangkok")); %>
  <joda:dateTimeZone value="${bkk}">
    <joda:format value="${datetime}" />
  </joda:dateTimeZone>

Attributes
value (required) Must be a DateTimeZone object, or an id of a zone

SetDateTimeZone tag

This tag sets the default time zone, either for the remainder of the page, or to a variable. If var is not specified, the zone will be stored in a scoped variable called "org.joda.time.dateTimeZone". This acts as a default for all the other tags. It will be overridden by a <joda:datetimezone /> tag, or by an explicit dateTimeZone attribute on <joda:format />.

Example:

  <% pageContext.setAttribute("now", new DateTime()); %>
  <% pageContext.setAttribute("bkk", DateTimeZone.forID("Asia/Bangkok")); %>
  <joda:dateTimeZone value="${bkk}" />
  <joda:format value="${datetime}" />

Attributes
value (required) Must be a DateTimeZone object, or an id of a zone
var The scoped variable to set
scope The scope of the variable to set