User Macro Confluence 7.4 - Can't apply method to $content.lastModificationDate data type
Asked by Joe Johnson (Inf):

Hello,

I have a user macro (developed by someone else who is no longer available to help debug) which worked in Confluence 6.13.7 but no longer works in 7.4. For context, I'm a Confluence administrator but do not understand Velocity in-depth.  

I've determined where the issue is and probably what's happening, but I don't know how to fix it:

```
#set($review=$content.currentDate)
#set($valid=$content.lastModificationDate)
#set($diff=$review.getTime()-$valid.getTime())
```

The above code is the offender: the third line uses the .getTime() method on $review and $valid to return the unix time of each date and compare them to finally return a number of milliseconds difference between the two ($diff).  

This works fine in 6.13 but in 7.4 it appears that the .getTime() method no longer works on the $valid variable. I suspect that it's because the data type is wrong or something like that. Is it possible to somehow convert the data so I can apply the method to it?  

I wrote a very basic macro just to output the contents to the screen for troubleshooting:  

```
## @noparams
#set($review=$content.currentDate)
#set($valid=$content.lastModificationDate)
#set($diff=$review.getTime()-$valid.getTime())

<h2>Vanilla</h2> <br>
$review <br>
$valid <br>
$diff <br>
<br>

<h2>GetTime Method Applied</h2> <br>
$review.getTime() <br>
$valid.getTime() <br>
$diff <br>
<br>
```

As an example, this is the output that I get:
>
> Vanilla {#toc-hId--1329382268}
> ------------------------------
>
> <br />
>
> Tue Jun 30 10:31:02 CEST 2020  
> 2020-06-30 09:29:37.283  
> $diff  
>
> GetTime Method Applied {#toc-hId-1158130565}
> --------------------------------------------
>
> <br />
>
> 1593505862585  
> $valid.getTime()  
> $diff  

What's really strange is that when I created this same troubleshooting macro in 6.13, the output in the vanilla section for $valid \*looks\* the same format in both versions of confluence - e.g. \*2020-06-30 09:29:37.283\* but the data is obviously not the same type.  

Any help appreciated!


Answers:
author: Joe Johnson (Inf)
body: Hello all,

In the end I found this post which describes the same issue I was having it looks like a change due to a security issue caused this unexpected behaviour. Atlassian are aware and apparently looking into a fix.  

<https://community.developer.atlassian.com/t/velocity-compare-2-dates/38614>  

In the meantime, based on the answers given in the above post, I managed to create a workaround. The post above just wanted to check whether two dates match, whereas I needed to check the difference (i.e. \<30 days and \>365 days):  

<br />

```
#set($modified = $content.lastModificationDate)
#set($review30 = $action.dateFormatter.calendar)
#set($review365 = $action.dateFormatter.calendar)

$review30.setTime($content.currentDate)
$review365.setTime($content.currentDate)

$review30.add(6, -30)
$review365.add(6, -365)

#if ($review30.getTime().compareTo($modified) < 1) Less than 30 days
#elseif ($review365.getTime().compareTo($modified) > -1) Greater than 365 days
#end
```

<br />

This allows me to check $modified against $review30 and $review365 and output (1, 0, -1) depending on the result, and do something, in the above case simply print a message.  

Hopefully this will be helpful to someone who comes across the same issue.  

Thanks!


author: Danyal Iqbal
body: all public methods are accessible in velocity, just like java. check public methods with the following docu and see if your time method is still there:

context.put('class', new ClassTool());

<http://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/ClassTool.html>

or some trial and error with:

<https://docs.atlassian.com/DAC/javadoc/confluence/4.2.4/reference/com/atlassian/confluence/core/DateFormatter.html>

e.g

```
$action.dateFormatter.formatDateTime($content.getLastModificationDate())
```

comments: author: Joe Johnson (Inf)
body: Thanks! I've done quite a lot of digging in the reference documentation and to be honest, as I'm not an engineer by trade it I tend to find the API reference docs pretty arcane. I enlisted some help from an engineer friend, and eventually found a solution - see below


Tags: date