java java日期何时会崩溃?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4195027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:15:31  来源:igfitidea点击:

When will the java date collapse?

javadatetime

提问by Denys S.

AFAIK java stores dates in long variables as milliseconds. Consequently someday there will be no value (cause long has a maximum) which will correspond to the time of that instant. Do you know when it will happen?

AFAIK java 将日期以毫秒为单位存储在长变量中。因此,有一天将没有与该时刻的时间相对应的值(因为 long 有最大值)。你知道什么时候会发生吗?

回答by Jon Skeet

It's easy enough to find out:

很容易发现:

public class Test {
    public static void main(String[] args) {
        System.out.println(new java.util.Date(Long.MAX_VALUE));
    }
}

Gives output (on my box):

给出输出(在我的盒子上):

Sun Aug 17 07:12:55 GMT 292278994

You may need to subtract a bit from Long.MAX_VALUEto cope with your time zone overflowing the range of long, but it will give a reasonable ballpark :)

您可能需要从中减去一点Long.MAX_VALUE以应对您的时区溢出 long 的范围,但它会给出一个合理的大概范围:)

回答by aioobe

According to the current leap-yearregulations the average number of days per year will be

根据现行的闰年规定,每年的平均天数为

         365 + 1/4 − 1/100 + 1/400 = 365.2425 days per year

         365 + 1/4 − 1/100 + 1/400 = 365.2425 天/年

This means that we, in average, have 31556952000 milliseconds per year.

这意味着我们平均每年有 31556952000 毫秒。

The long-value represents the number of milliseconds since the Epoch(1st of January, 1970) and the maximum number represented by a Java long is 263− 1, so the following calculation

long 值表示自Epoch(1970 年 1 月 1 日)以来的毫秒数,Java long 表示的最大数为 2 63− 1,因此以下计算

         1970 + (263− 1) / 31556952000

         1970 + (2 63− 1) / 31556952000

reveals that this representation will overflow year 292278994.

表明此表示将溢出 292278994 年。



This can, as Jon Skeet points out, be confirmed by

正如Jon Skeet 所指出的,这可以通过

-> System.out.println(new Date(Long.MAX_VALUE));
Sun Aug 17 08:12:55 CET 292278994