Search code examples
grailsgroovytimedurationcontent-expiration

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


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?


Solution

  • 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.