java.util.Date 的有效范围?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5488038/
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 11:21:23  来源:igfitidea点击:

Valid range for java.util.Date?

javadatejvm

提问by Dexter

what is the range for valid values that I can store in java.util.Date? The APIdoesn't say much about this.

我可以在 java.util.Date 中存储的有效值的范围是多少?该API没有说太多关于这一点。

Or does it only support dates that can be expressed as unix timestamps (that is dates after 1.1.1970)? If so, is there maybe a (serializeable) class in the JDK that supports also dates prior to that?

或者它是否只支持可以表示为 unix 时间戳的日期(即 1.1.1970 之后的日期)?如果是这样,JDK 中是否有一个(可序列化的)类也支持之前的日期?

What I'm looking for is a class/type for a birthday-field in db4o

我正在寻找的是 db4o 中生日字段的类/类型

回答by Rob Hruska

It supports dates between Long.MIN_VALUE and Long.MAX_VALUE:

它支持 Long.MIN_VALUE 和 Long.MAX_VALUE 之间的日期:

class DateTest {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("d MMM yyyy G, HH:mm:ss.S Z");

        System.out.println(df.format(new Date(Long.MIN_VALUE)));
        System.out.println(df.format(new Date(0)));
        System.out.println(df.format(new Date(Long.MAX_VALUE)));
    }
}

Outputs

输出

2 Dec 292269055 BC, 10:47:04.192 -0600
31 Dec 1969 AD, 18:00:00.0 -0600
17 Aug 292278994 AD, 01:12:55.807 -0600

(Note: times above are Central Time)

(注:以上时间为中部时间)

回答by thkala

java.util.Datestores dates in a longas milliseconds using 1970-01-01 as a reference. Since longis a signed 64-bit integer, you can expect java.util.Dateto cover about 290 million years before and after the reference date - that is if you don't care about accurate representation and calendar system switches.

java.util.Datelong使用 1970-01-01 作为参考以毫秒为单位存储日期。由于long是一个有符号的 64 位整数,因此您可以预期java.util.Date在参考日期之前和之后覆盖大约 2.9 亿年 - 也就是说,如果您不关心准确的表示和日历系统切换。

Unless you are planning a birthday party for a dinosaur, I'd say that java.util.Date is probably fine for your purpose...

除非您计划为恐龙举办生日派对,否则我会说 java.util.Date 可能适合您的目的......

回答by Riccardo Cossu

Dates can contain values before 1.1.1970, just use negative long :-)

日期可以包含 1.1.1970 之前的值,只需使用负 long :-)