Outputing day after value in field

I’m creating links in an event page that go to Google Calendar’s add to calendar function. These have a dates parameter of the form dates=yyyymmdd/yyyymmdd where the start day of the event and the second date is the day AFTER the end day of the event.

So a one day event happening today would be dates=20081205/20081206

My content type has two date fields: a mandatory event_start_date and an optional event_end_date. If the optional field is empty the event is assumed to be a single day event.

So I need to make my velocity template output the yyyymmdd that is one day after the event_end_date if it is set otherwise one day after event_start_date.

A Java programmer colleague has suggested using
calendar.add(Calendar.DAY_OF_MONTH, 1) but I can’t seem to work out how to use that correctly within the velocity template.

Any ideas?

You should be able to find something that’ll work for you here:

http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DateTool.html
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/MathTool.html

I’ve done something similar:

#set($daybefore =  $tools.math.sub($tools.date.getDay($sys.item.getProperty("rx:event_start").Date), 1))##

Just change the sub method call to an add to get the following day instead. Also edit to use whatever date field you want to base the calculation on (it should be a sys_CalendarSimple type control.)

Fortunately for me, I’ve only ever used stuff like this to build date strings for in JSR-170 queries, which doesn’t quibble if $daybefore is zero, as will happen on the first of every month. But you’ll probably need some IF statements to check for the last day of every month. If Google also expects two digits for the day, you’ll also have to prefix with a zero for the first nine days of every month.

Andrew.

Thanks Andrew,

Treating the components of the date as numbers is very much a last resort. I’d much rather manipulate the date object itself as Java can take care of not only the last day of the month, but also the last day of the year and leap years, for me.

Steve

Maybe I’m not seeing the problem here, but $tools.DateTool.toCalendar() will return a java.util.Calendar object. The input can be a String or a java.util.Date instance. You can use the Calendar.add() method to add 1 day (as Andrew suggests). This will handle “rollovers” at the end of the month automatically, there’s nothing you need to do.

You can call $tools.DateTool.format(). A format string of “yyyyMMdd” should return the date in the format you are looking for.

Is there some problem that I’m overlooking here?

The problem is that I already know that ideally I want to use calendar.add() as my colleague suggested but I don’t know how to.

Andrew’s suggestion of using math.add() rather than calendar.add() would leave me with extra work to do.

So would this syntax work? I’m at home now and can’t test it until Monday.


#set($nextday = $tools.DateTool.format($tools.DateTool.toCalendar($sys.item.getProperty("rx:event_end_date")).add(Calendar.DAY_OF_MONTH, 1)), 'yyyyMMdd')

No, I’m afraid it’s slightly more complicated than that.


#set($start=$tools.date.toCalendar($sys.item.getProperty("rx:sys_contentstartdate").Date))##
<p>Start date: $tools.date.format("yyyyMMdd", $start)</p>##
$start.add(5,10)##
<p>10 days later: $tools.date.format("yyyyMMdd", $start)</p>


#set($today=$tools.date.getCalendar())##
<p>Today: $tools.date.format("MMM dd, yyyy", $today)</p>	
$today.add(5,1)##
<p>Tommorrow: $tools.date.format("MMM dd, yyyy", $today)</p>

will produce


Start date: 20080929

10 days later: 20081009

Today: Dec 06, 2008

Tommorrow: Dec 07, 2008

The constant value “DAY_OF_MONTH” is 5. In Rhythmyx 6.6 (or if you’ve already patched your system to upgrade Velocity tools), you will be able to use $tools.field to obtain these constants.

The JavaDoc for Calendar will let you look up the constants easily enough.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

Also, the format strings are copied from the Java simple date format:
http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html

If you need a Locale other than English, see this thread
http://forum.percussion.com/showthread.php?t=423

I hope this helps

Dave

Thank you, that works a treat.

I now have an event template that outputs correctly formatted links to both Google Calendar and our own iCal generator, and also has hCalendar microformat data embedded within it.