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