Java中的最小日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3838242/
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
Minimum date in Java
提问by user463745
What is the minimum date value in Java?
Java中的最小日期值是多少?
回答by mikej
If you are talking about java.util.Date
as a timestamp you can do this
如果你说的java.util.Date
是时间戳,你可以这样做
Date d = new Date(0L)
and you will see this represents Thu Jan 01 01:00:00 GMT 1970
Date d = new Date(0L)
你会看到这代表 Thu Jan 01 01:00:00 GMT 1970
As tulskiyhas pointed out it is possible to pass a negative value to the Date constructor. If we do this and use a date format that includes the era we can see:
正如tulskiy所指出的,可以将负值传递给 Date 构造函数。如果我们这样做并使用包含时代的日期格式,我们可以看到:
Date d = new Date(Long.MIN_VALUE);
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy G HH:mm:ss Z");
System.out.println(df.format(d));
displays: Sun, 2 Dec 292269055 BC 16:47:04 +0000
显示时间:公元前 292269055 日,星期日 16:47:04 +0000
回答by Denis Tulskiy
Don't forget that Date constructor happily accepts negative values.
不要忘记 Date 构造函数愉快地接受负值。
Date date = new Date(Long.MIN_VALUE);
returns
返回
Sun Dec 02 22:47:04 BDT 292269055
12 月 2 日星期日 22:47:04 BDT 292269055
I guess that's about the time of Big Bangdinosaurs :)
我想那是关于大爆炸恐龙的时代:)
EDIT
编辑
As martin claytonanswered, you can use the Calendar class to check the era. This will output 0 which stands for BCE:
正如马丁克莱顿回答的那样,您可以使用 Calendar 类来检查时代。这将输出代表 BCE 的 0:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(Long.MIN_VALUE));
System.out.println(calendar.get(Calendar.ERA));
回答by martin clayton
It's the same as for the Calendar classes.
它与 Calendar 类相同。
Try this:
尝试这个:
Date d = new Date( Long.MIN_VALUE );
System.out.println( d );
You'll see:
你会看到的:
Sun Dec 02 16:47:04 GMT 292269055
But the default date format doesn't include the era - which is BCEfor this date.
但是默认的日期格式不包括时代——这是这个日期的BCE。
回答by Basil Bourque
The other Answers may be correct but use outmoded classes.
其他答案可能是正确的,但使用过时的类。
java.time
时间
The old date-time classes (java.util.Date/.Calendar etc.) have been supplanted by the java.timeframework built into Java 8 and later.
旧的日期时间类(java.util.Date/.Calendar 等)已被Java 8 及更高版本中内置的java.time框架所取代。
The java.time classes are inspired by Joda-Time, defined by JSR 310, extended by the ThreeTen-Extra project, back-ported to Java 6 & 7 by the ThreeTen-Backportproject, and adapted to Android in the ThreeTenABPproject. See Tutorial.
的java.time类由启发约达时间,由下式定义JSR 310,由ThreeTen-额外项目,由后移植到Java 6和7延伸ThreeTen-反向移植项目,并在适合于的Android ThreeTenABP项目。请参阅教程。
For a moment on the timeline in UTCwith a resolution of nanoseconds, use Instant
. Given an offset-from-UTC, use OffsetDateTime
. For a time zone(offset + rules for anomalies), use ZonedDateTime
, but by its nature has no defined minimum, nor does ZoneId
. For a date-only value without time-of-day and without time zone, use LocalDate
. For a time-of-day only value without date and without time zone, use LocalTime
. For date-time without time zone, use LocalDateTime
.
在UTC时间线上,分辨率为纳秒,使用Instant
. 给定一个offset-from-UTC,使用OffsetDateTime
. 对于时区(偏移量 + 异常规则),使用ZonedDateTime
,但本质上没有定义最小值,也没有ZoneId
。对于没有时间和时区的仅日期值,请使用LocalDate
. 对于没有日期和时区的仅时间值,请使用LocalTime
。对于没有时区的日期时间,请使用LocalDateTime
.
Instant.MIN
=?-1000000000-01-01T00:00Z
OffsetDateTime.MIN
=-999999999-01-01T00:00:00+18:00
LocalDate.MIN
=-999999999-01-01
LocalTime.MIN
=00:00
LocalDateTime.MIN
=-999999999-01-01T00:00:00
Year.MIN_VALUE
=-999,999,999
ZoneOffset.MIN
=-18:00
(but-12:00
in practice)
Instant.MIN
=?-1000000000-01-01T00:00Z
OffsetDateTime.MIN
=-999999999-01-01T00:00:00+18:00
LocalDate.MIN
=-999999999-01-01
LocalTime.MIN
=00:00
LocalDateTime.MIN
=-999999999-01-01T00:00:00
Year.MIN_VALUE
=-999,999,999
ZoneOffset.MIN
=-18:00
(但-12:00
在实践中)
Caution:Be wary of using these values as some kind of flag or special meaning. Many other software libraries and databases may not support these extreme values.
注意:小心使用这些值作为某种标志或特殊含义。许多其他软件库和数据库可能不支持这些极端值。
For a flag or special meaning such as a non-null "no value available", I suggest picking an arbitrary moment but avoid going to such extreme reaches either backward or forward in time. Perhaps the Unix epoch reference date, the first moment of 1970 in UTC, 1970-01-01T00:00:00Z. Defined as a constant in Java: Instant.EPOCH
对于标志或特殊含义,例如非空的“无可用值”,我建议选择任意时刻,但要避免在时间上向后或向前到达如此极端的范围。也许Unix 纪元参考日期,UTC 中的 1970 年的第一个时刻,1970-01-01T00:00:00Z。在 Java 中定义为常量:Instant.EPOCH
回答by deadManN
Since Date
is marked as Deprecated, i thought there should be another way to get around this, so i looked at some of the methods, and i found this way
由于Date
被标记为 Deprecated,我认为应该有另一种方法来解决这个问题,所以我查看了一些方法,我找到了这种方法
You can aslo use Long.MIN_VALUE, at the time i wrote this, i hadn't searched for it, and even/so i didn't remember it.
回答by Shashidhar Reddy
LocalDateTime MIN defines the minimum supported LocalDateTime, '-999999999-01-01T00:00:00'. This is the local date-time of midnight at the start of the minimum date.
LocalDateTime MIN 定义了支持的最小 LocalDateTime,'-999999999-01-01T00:00:00'。这是最小日期开始时午夜的本地日期时间。
public static final LocalDateTime MIN; this is the MIN syntax;
public static final LocalDateTime MIN;这是 MIN 语法;
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime a = LocalDateTime.MIN;
System.out.println(a);
}
}