gpt4 book ai didi

scala - 如何在 Scala 中将 Long 转换为 Int?

转载 作者:行者123 更新时间:2023-12-03 08:20:50 26 4
gpt4 key购买 nike

我想使用以下函数将 Joda 时间转换为 Unix 时间戳:


def toUnixTimeStamp(dt : DateTime) : Int = {
val millis = dt.getMillis
val seconds = if(millis % 1000 == 0) millis / 1000
else { throw new IllegalArgumentException ("Too precise timestamp") }

if (seconds > 2147483647) {
throw new IllegalArgumentException ("Timestamp out of range")
}

seconds
}

我打算获得的时间值永远不会精确到毫秒,它们是合约上精确到秒的 UTC,并且将进一步存储(在 MySQL 数据库中)作为 Int,标准 Unix 时间戳是我们公司的时间记录标准。但是 Joda Time 只提供 getMillis 而不是 getSeconds,所以我必须得到一个 Long 毫秒级的时间戳并将它除以 1000 以生成一个标准的 Unix 时间戳。

我坚持让 Scala 把一个 Long 值变成一个 Int 。这样的 Actor 怎么演?

最佳答案

使用 .toInt Long 上的方法,即 seconds.toInt

关于scala - 如何在 Scala 中将 Long 转换为 Int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7782502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com