Getting time difference


i’m trying to get a time different between now and some future date string that i receive from somewhere else…
like this:

i have a string of future date input in this format:

so now i would like to get the time from now to my future date string input…
there are so many approach for this (you can suggest me the best approach) but what i can think of is:
convert both input and now to unix milisecond time, minus it then convert it back again to time…

question is: is my approach will be work?

and i got a problem parsing the input string…
i did:

def dateObject= Date.parse(“yyyy-MM-dd’T’HH:mm:ss’Z’”, “2015-10-08T11:01:29+00:00”)
but it return me an error: error java.text.ParseException: Unparseable date: “2015-10-08T11:01:29+00:00” @ line 29226

anyone have experince getting a time different?

my expected outcome later will be:
from now to the future date is: 3 hours 23 mins 51 secs

any advise :blush:



Hi @sidjohn1,

thanks for your reply,
i did tried that, but i got this error:

Java.lang.SecurityException: Invoking methods on class java.lang.Class is not allowed: minus

which refer to code below:

TimeDuration duration = TimeCategory.minus(now, start)

i did put below as well above my “definition” part

import groovy.time.*

… what’s possibly went wrong?
any idea? :smiley:


I solve it with:

def parse(String s){
    long timeDiff
    def now = new Date()
    def end = Date.parse("yyy-MM-dd'T'HH:mm:ssZ","${s}".replace("+00:00","+0000"))
    long unxNow = now.getTime()
    long unxEnd = end.getTime()
    unxNow = unxNow/1000
    unxEnd = unxEnd/1000
    timeDiff = Math.abs(unxNow-unxEnd)
    timeDiff = Math.round(timeDiff/60)
    return timeDiff

Thanks :smiley:

1 Like

@RHQB2B Nice… I’ll be using this as well. Thanks.

1 Like