如何在Scala中计算带有TTL的Unix时间戳

huangapple go评论96阅读模式
英文:

how to calculate unixtime stamp with ttl in scala

问题

  1. 嗨,我想要计算一个带有 24 小时生存时间(TTL)的 Unix 时间戳,以便我可以将其用作一天的到期时间。为此,我经过一些研究尝试了以下代码。
  2. 这是我在 Scala 上的代码:
  3. import java.time.Instant
  4. import java.util.Date
  5. val ttl: Long = 24 * 3600
  6. val unixTimeStamp = System.currentTimeMillis() / 1000L + ttl
  7. 上述代码会产生输出:
  8. 1602071235
  9. 我只是想知道这是否是在 Scala/Java 中正确完成此操作的方式。
英文:

hi i want to calculate unixtime stamp with a ttl of 24 hours so that i can use it as a expiry time of 1 day for that i have tried the following code after doing some research

here is my code on scala

  1. import java.time.Instant
  2. import java.util.Date
  3. val ttl:Long =24*3600
  4. val unixTimeStamp =System.currentTimeMillis()/1000L + ttl

the above produces the output

  1. 1602071235

i just want to know is that the correct way of doing this in scala/java

答案1

得分: 1

使用在Java 8中引入的java.time API,您只需执行以下操作:

  1. import java.time.{Duration, Instant}
  2. Instant.now.plus(Duration.ofDays(1)).toEpochMilli
英文:

Using the java.time API introduced in Java 8 you only need to do this:

  1. import java.time.{Duration, Instant}
  2. Instant.now.plus(Duration.ofDays(1)).toEpochMilli

huangapple
  • 本文由 发表于 2020年10月6日 20:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64225643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定