将时间转换为Scoda中的日期时间

编程入门 行业动态 更新时间:2024-10-27 06:21:50
本文介绍了将时间转换为Scoda中的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图从时代开始需要几秒钟,并将其转换成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中的日期时间

本文发布于:2023-10-23 06:19:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   转换为   日期   Scoda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!