Tiny
Tiny

Reputation: 27899

Formatting and parsing Joda-Time based date and time objects in JSP

I'm using joda-time-jsptags-1.1.1 to format and parse Joda-Time based date and time objects on JSP pages with the following taglib.

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

I have dates in Oracle database which are stored using the standard UTC format.

Using the following JSTL tags,

<c:set var="startDate" value="${row.discountStartDate}"/>
<c:set var="endDate" value="${row.discountEndDate}"/>

these two dates are displayed in the following format respectively.

2013-02-16T22:31:59.000+05:30 
2013-02-28T22:32:03.000+05:30   

I need to display these dates in a specific format using a specific time zone. I'm trying to parse these dates as follows.

<joda:parseDateTime var="startDate"
                    pattern="dd-MMM-yyyy HH:mm:ss"
                    value="${row.discountStartDate}"
                    dateTimeZone="Asia/Kolkata"/>

<joda:parseDateTime var="endDate"
                    pattern="dd-MMM-yyyy HH:mm:ss"
                    value="${row.discountStartDate}"
                    dateTimeZone="Asia/Kolkata"/>

I expect these dates to be parsed in the given format and zone. I'm however, getting the following exception.

java.lang.IllegalArgumentException: Invalid format: "2013-02-16T22:31:59.000+05:30" is malformed at "13-02-16T22:31:59.000+05:30"

How to parse these dates

Upvotes: 1

Views: 3091

Answers (1)

Tiny
Tiny

Reputation: 27899

The dates returned via Hibernate by row.discountStartDate and row.discountEndDate in this example are obviously the type of org.joda.time.DateTime.

These properties are designated by org.jadira.usertype.dateandtime.joda.PersistentDateTime in the respective entity class like,

@Column(name = "DISCOUNT_START_DATE")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime discountStartDate;

@Column(name = "DISCOUNT_END_DATE")    
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime discountEndDate;

Therefore, there is no need to parse these date once again. I just need to format these dates with a given format and time zone, just like the following.

<joda:format var="startDate"
             value="${row.discountStartDate}"
             pattern="dd-MMM-yyyy HH:mm:ss"
             style="F-"
             dateTimeZone="Asia/Kolkata"/>

<joda:format var="endDate"
             value="${row.discountEndDate}"
             pattern="dd-MMM-yyyy HH:mm:ss"
             style="F-"
             dateTimeZone="Asia/Kolkata"/>

The value attribute of the format tag must be of type ReadableInstant or ReadablePartial

The style attribute is the style to use for parsing. It specifies two characters, one for date, one for time.

S = Short
M = Medium
L = Long
F = Full
- = None

http://joda-time.sourceforge.net/contrib/jsptags/userguide.html

Upvotes: 5

Related Questions