How can I count expiration date in Grails, if I know the amount of time left?

161 Views Asked by At

I want to insert expiration date in Redis based on TTL. How can I count the expiration date?

I'm trying to use TimeCategory class, here is the example:

def ttl = 3600;
def date = new Date()

TimeDuration duration = getSeconds(ttl) 

TimeDuration expiryDate = date.plus.duration

Is that a correct approach to expiry date counting?

1

There are 1 best solutions below

7
On BEST ANSWER

Overcomplicated if you ask me.

One-liner should be sufficient here:

Date expiryDate = new Date( System.currentTimeMillis() + ttlInSeconds * 1000l )

Make sure you are using long numbers here, otherwise the numbers will be cut down to 2147483647 which might lead to wrong results for large TTLs.