System.currentTimeMillis 的长度
System.currentTimeMillis
是否始终返回固定长度的值。在我的 Windows Core2 中,它返回一个 13 位长的值。
从它的API:
返回当前时间(以毫秒为单位)。请注意,虽然返回值的时间单位是毫秒,但该值的粒度取决于底层操作系统,并且可能会更大。例如,许多操作系统以数十毫秒为单位测量时间。
Does System.currentTimeMillis
always returns a fixed length of value. In my windows Core2, it return a 13 digit long value.
From its API:
Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
System.currentTimeMillis()
返回自纪元以来的毫秒数,即自 1970 年 1 月 1 日午夜 UTC 以来的毫秒数。您可以检查自纪元以来的毫秒数第一次达到 13 位十进制数字。这件事发生在
您还可以检查自纪元起的毫秒数最后一次为 13 位十进制数字。这将发生在
因此,在这两个日期之间,假设机器正确设置了当前时间,该函数将始终返回 13 位十进制数字值。
因此,您可以安全地假设返回值在接下来的两个多世纪内都是 13 位十进制数字。
System.currentTimeMillis()
returns the number of milliseconds since epoch, i.e. since midnight UTC on the 1st January 1970.You can check when the the number of milliseconds since epoch was 13 decimal digits for the first time. This happened on
You can also check when the number of milliseconds since epoch is going to be 13 decimal digits for the last time. This is going to happen on
Thus between these two dates, the function will always return 13 decimal digit value assuming the machine has the current time set correctly.
So you're safe with the assumption that the return value is 13 decimal digits for more than the next two centuries.
它返回一个 63 位二进制数(它实际上是一个 64 位有符号数,始终为正数,因此从不设置最高位)。许多前导数字为零。当您将其转换为十进制时,通常会丢弃任何前导零。因此,小数位数会有所不同。
It returns a 63-digit binary number (it's actually a 64-bit signed number which is always positive, so the top bit is never set). Many of the leading digits will be zero. When you convert it to decimal, any leading zeroes are usually discarded. So, the number of decimal digits will vary.
投票最多的答案(即从 2001-09-09T01:46:40 UTC 到 2286 范围内的任何日期的纪元毫秒-11-20T17:46:39.999 UTC 将有 13 位数字)是准确的。对于任何有兴趣了解如何得出这些数字的人来说,这是对该答案的补充。
通常,我首先使用现代日期时间 API 编写解决方案,但在本例中,我将反转该模式,因为现代日期时间 API
java.time
于 2014 年 3 月发布(2使用旧版日期时间 API:
使用
java.time
,现代日期时间 API:输出中的
Z
是时区指示符 表示零时区偏移。它代表 Zulu 并指定。 Etc/UTC
时区(时区偏移为+00:00
小时)了解有关
java.time
(现代日期时间 API*< /sup> 来自跟踪:日期时间。* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API 和 如何在Android项目中使用ThreeTenABP。
The most voted answer (i.e. the epoch milliseconds for any date ranging from 2001-09-09T01:46:40 UTC to 2286-11-20T17:46:39.999 UTC will have 13-digits) is spot-on. This is a supplement to that answer for anyone interested to know how one may arrive at those figures.
Usually, I write the solution using the modern date-time API first but in this case, I will reverse the pattern because
java.time
, the modern date-time API was released in Mar-2014 (2 years after the most voted answer was posted.Using the legacy date-time API:
Using
java.time
, the modern date-time API:The
Z
in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies theEtc/UTC
timezone (which has the timezone offset of+00:00
hours).Learn more about
java.time
, the modern date-time API* from Trail: Date Time.* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.