从天转换为毫秒
我想创建一个将天数转换为毫秒的函数。天的格式存储为0.2444,那么如何将其转换为毫秒呢?
I want to create a function that will convert the days into milliseconds. The days format is stored as 0.2444, so how to convert this to milliseonds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为最好的做法是:
The best practice for this, in my opinion is:
除了其他答案之外,还有 TimeUnit 类允许您将一个持续时间转换为另一个持续时间。例如,要找出一天由多少毫秒组成:
请注意,此方法需要很长的时间,因此如果您有一天的一小部分,则必须将其乘以毫秒数有一天。
In addition to the other answers, there is also the TimeUnit class which allows you to convert one time duration to another. For example, to find out how many milliseconds make up one day:
Note that this method takes a
long
, so if you have a fraction of a day, you will have to multiply it by the number of milliseconds in one day.天 * 24 * 60 * 60 * 1000
还不够吗?Won't
days * 24 * 60 * 60 * 1000
suffice?24 小时 = 86400 秒 = 86400000 毫秒。只需将您的数字乘以 86400000 即可。
24 hours = 86400 seconds = 86400000 milliseconds. Just multiply your number with 86400000.
值得一提的是,这种方法每 4-5 年可能会出现 1 秒的错误,因为存在闰秒 (http://www.nist.gov/pml/div688/leapseconds.cfm),当天的正确公式为:
有一个问题 日历是否支持闰秒? 答案是否定的。
因此,如果您正在设计超级依赖时间的软件,请小心这个公式。
Its important to mention that once in 4-5 years this method might give a 1 second error, becase of a leap-second (http://www.nist.gov/pml/div688/leapseconds.cfm), and the correct formula for that day would be
There is a question Are leap seconds catered for by Calendar? and the answer is no.
So, if You're designing super time-dependant software, be careful about this formula.
您可以使用此实用程序类 -
如果您正在使用Android框架,那么只需
导入
它(也称为DateUtils
) 在package android.text.format
下You can use this utility class -
If you are working on Android framework then just
import
it (also namedDateUtils
) underpackage android.text.format
或者作为
long
:or as
long
:对 E_x 答案进行一些修改:
示例: 致电 1 天:
Some Modification of E_x Answer :
Example : Call for 1 day :