Velocity compare 2 dates

Hi all

I had a good solution to compare two dates in a confluence user macro.
I checked both dates with the millisecond timestamp.

#set( $modDateTimeMillis = $childVersion.getLastModificationDate().getTime() )
#if( $modDateTimeMillis > $startDate.timeInMillis)
#set( $dummy = $allVersions.add($childVersion) )
#end

But since the update to confluence 7.4 the “getTime()” function gives no return value.
How can I get the milliseconds from a date?
or even better:

Does anybody know, how I can compare two dates in a confluence user macro? The DateTool of Velocity dosen’t seems to work in confluence.

Regards, Dominic

1 Like

Hi @DominicLagger,
I will try to get some help for you.
Regards,
Dugald

1 Like

The Confluence server team have confirmed that the impact of this change was not intended. They’re looking into a workaround, but will also be able to rectify in a bugfix release.

1 Like

Hi Dominic,

Please try using Date’s compareTo function. It should work, depending on where startDate comes from.

#if($startDate.compareTo($childVersion.getLastModificationDate()) < 0)

(or > 0 if I’ve got that backwards )

Note that the other way round will not work for the same reason that getTime() is broken.

## this won't work
$childVersion.getLastModificationDate().compareTo($startDate)

Thanks for finding the bug!
Don

2 Likes

Hi @DominicLagger,

I would highlight one more point here on top of @DonWillis’ mentioned workaround.
The reason your date object is not working because we have places restrictions on java.sql.* usage in velocity files to security issues. This was restricted in 7.0.1.

Since the object returned from childVersion.getLastModificationDate() is a java.sql.Date instance, your getTime() has stopped working.

Please let us know if you are okay with the workaround.

Thanks,
Ganesh

Thanks a lot for all the suppot!
Is there somewhere I can watch the ticket?

First, I have to explain where the startDate comes from:
The startDate comes from a parameter in the user macro, which gives a german date in a string format.

#set( $startDate = $action.dateFormatter.getCalendar())
## Parse the parameter, which is a date in german format like "28.05.2020"
$startDate.set($year, $month, $day, 0, 0, 0)

This gives me a calendar date. The Calendar API says, that the compareTo method needs Calendar parameter. But the “getLastModificationDate()” returns a Date, not a Calendar.

What I did:
I get the Date from the Calendar with:

$startDate.getTime()
#Returns a  `Date`  object representing this  `Calendar` 's time value (millisecond offset from the [Epoch](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html?is-external=true#Epoch)").

Now I can compare the two Dates with

#if( $startDate.getTime().compareTo($childVersion.getLastModificationDate()) )

And that worked for me!
Thanks a lot for the hints!

Regards, Dominic

3 Likes

Hi,

I am now having some issues with the java.sql.timestamp object which is created when running issue.getUpdated() function, in VTL.

The $issue.getUpdated() function returns a timestamp. I want to compare the issue.getUpdated() with a linked issue timestamp; $linked_issue.getUpdated(). However, as mentioned above i cannot run any function on the timestamp object after version 7.0.1.

I wonder if you have a workaround for comparing update timestamp of two issues?

I solved this by having to convert the timestamps into Date objects in the java backend. Then send all the Date objects of each issue to the VTL by using a map.