如果我知道剩余的时间,我如何计算Grails的到期日期?

问题描述 投票:0回答:1

我想基于TTL在Redis中插入到期日期。我如何计算到期日期?

我正在尝试使用TimeCategory类,这里是一个例子:

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

TimeDuration duration = getSeconds(ttl) 

TimeDuration expiryDate = date.plus.duration

这是到期日期计算的正确方法吗?

grails groovy time duration content-expiration
1个回答
2
投票

如果你问我,过于复杂。

这里单线应该足够了:

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

确保你在这里使用long数字,否则数字将被减少到2147483647,这可能导致大TTL的错误结果。

© www.soinside.com 2019 - 2024. All rights reserved.