Velocity compare 2 dates
Asked by Dominic Lagger:

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


Answers:
author: Dominic Lagger
body: The Developer community helped a lot! Also thank you [@Diego](/t5/user/viewprofilepage/user-id/1188952) for pointing to the developer community!

Here is the post and the answer: <https://community.developer.atlassian.com/t/velocity-compare-2-dates/38614>

**My answer**
> Thanks a lot for all the support!
>
> **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!

Regards, Dominic

comments: author: Diego
body: Hey, Thanks for getting back here to share the solution. Wonderful!


author: Diego
body: Hello [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) !

The behavior you described "But since the update to confluence 7.4 the "getTime()" function gives no return value." Is indeed strange.

I have yet to try this method, but I will try to and update this thread with the results.

You also specified that there is a need to compare two dates. I was able to find this thread here in our community that might be helpful:
> * ++***[\[Atlassian Community\] I would like to compare dates](https://community.atlassian.com/t5/Confluence-questions/I-would-like-to-compare-dates/qaq-p/61312)***++

Further, I believe that this page should be helpful:
> * ++***[\[Confluence Server\] Confluence objects accessible from Velocity](https://developer.atlassian.com/server/confluence/confluence-objects-accessible-from-velocity/)***++

Looking at the methods of the available objects, ++*some of them have been deprecated after Confluence 7.3.*++

There is also a bigger chance of you finding specialized help from our Developer community. Here, take a look:

> * ++***[\[Atlassian Developer Community\] Confluence](https://community.developer.atlassian.com/c/Confluence)***++

I hope this helps! Looking forward to your reply.

comments: author: Dominic Lagger
body: Hi [@Diego](/t5/user/viewprofilepage/user-id/1188952)

Thanks for your answer.

The post for comparing two dates doesn't helped me out. I'm not able to get it work properly.

The page with the accessible objects from Velocity I also found. But also there, I'm not able to find a proper solution.

I now created a post in the developer community. Unfotunatley I was blocked (My account is "temporarily on hold"...

Regards, Dominic


author: Diego
body: Hey, thanks for sharing this information with us [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) ! Would it be possible for you to ++*share the question title you used in our Developer Community*++ ?  
Also, are you using the same Atlassian Account in our Developer Community?

I will try to look into your current situation there.


author: Dominic Lagger
body: Hi [@Diego](/t5/user/viewprofilepage/user-id/1188952)

The post was approved a little bit later, so all good :)

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

Regards, Dominic


author: Diego
body: Glad to know!

Let us hear how it goes over there in our Sister Community.


Tags: confluence-server

server

user-macro

velocity