SOLVED

Velocity: creating two calendar objects?

Go to solution
Highlighted
Level 6

Velocity: creating two calendar objects?

Seeing something that seems a bit strange to me in Velocity. I'm trying to create two calendar objects to compare to. One is today's date. The other is 30 days in the future. Here's the code:

##set time zone and locale settings for date conversions
#set( $TimeZone = $date.getTimeZone().getTimeZone("America/New_York") )
#set( $Locale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $calFuture = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($TimeZone) )
#set( $ret = $calFuture.setTimeZone($TimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $calConst = $field.in($calFuture) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $FRIENDLY_DATETIME = "MMM dd" )
##add 30 days to today's date for comparison, and save it as a new variable
#set($calFuture = $calNow.add($calConst.DATE,30))


calendar now = $calNow
<br>
calendar future = $calFuture
<br>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The output I get from this is: 

calendar now = java.util.GregorianCalendar[time=1572490557808,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=9,WEEK_OF_YEAR=44,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=303,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=55,SECOND=57,MILLISECOND=808,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
calendar future =

So it looks like the first calendar object, the $calNow, is working just fine. But the second one, calendar future, is giving a blank. Why? Surely I can have more than one calendar object in the same script? I guess I just don't understand how these calendar objects work.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Level 10 - Community Moderator

Re: Velocity: creating two calendar objects?

Calendar.add has a void return value. So you're overwriting $calFuture with nothing.

You want

#set( $void = $calFuture.add($calConst.DATE,30) )

Also, this is repetitive:

#set( $calConst = $field.in($calNow) )
#set( $calConst = $field.in($calFuture) )‍‍

The fields are always the same and you're just reassigning $calConst. You only need the first line.

View solution in original post

3 REPLIES 3
Highlighted
Level 10 - Community Moderator

Re: Velocity: creating two calendar objects?

Calendar.add has a void return value. So you're overwriting $calFuture with nothing.

You want

#set( $void = $calFuture.add($calConst.DATE,30) )

Also, this is repetitive:

#set( $calConst = $field.in($calNow) )
#set( $calConst = $field.in($calFuture) )‍‍

The fields are always the same and you're just reassigning $calConst. You only need the first line.

View solution in original post

Highlighted
Level 6

Re: Velocity: creating two calendar objects?

As always, thanks Sanford! Works like a charm. I need to remember that those .add pieces always need a dummy variable!

Highlighted
Level 10 - Community Moderator

Re: Velocity: creating two calendar objects?

Yes, there's actually something strange/useful that Velocity does with void methods.  I'm gonna put a blog post up about that.

In any case, yes, my strong recommendation is #set the fake-keyword $void for void methods.