我试图从时代开始需要几秒钟,并将其转换成Scala中的DateTime对象。我使用吉达。不幸的是,无论我使用几秒还是几毫秒,我得到了奇怪的结果。我在这里做错了什么?
I am attempting to take seconds since epoch and turn it into a DateTime object in Scala. I am use joda. Unfortunately whether I use seconds or milliseconds, I'm getting weird results. What am I doing wrong here?
scala> new org.joda.time.DateTime(1378607203*1000) res2: org.joda.time.DateTime = 1969-12-31T02:31:40.984Z scala> new org.joda.time.DateTime(1378607203) res3: org.joda.time.DateTime = 1970-01-16T22:56:47.203Z推荐答案
检查快速REPL会话:
Check a quick REPL session:
scala> 1378607203 * 1000 res6: Int = -77299016奇怪,不是吗?你能猜到为什么会这样吗?
Odd, isn't it? :) Can you guess why this is happening?
我将给你一个从 DateTime 的构造函数提取的提示,你试图使用。
I will give you a hint extracted from DateTime's constructor you are trying to use.
DateTime(long instant)仍然没有得到它?让我们尝试一下略有不同的版本:
Still don't get it? Let's try a slightly different version:
scala> 1378607203L * 1000 res8: Long = 1378607203000注意 L 表示Long类型的文字。你要求1万亿!而Int只能达到2亿美元:
Notice the L indicating a literal of type Long. You are asking for 1 trillion! And Int only go as far as 2 billons:
scala> Int.MaxValue res7: Int = 2147483647所以做 DateTime (1378607203L * 1000)将使其正常工作。
So doing DateTime(1378607203L*1000) will make it work.
更多推荐
将时间转换为Scoda中的日期时间
发布评论