Java:GregorianCalendar 的最大值和最小值是什么/在哪里?
GregorianCalendar 的最大值和最小值是多少?
它们是像 Integer.MAX_VALUE 这样的常量,还是 GregorianCalendar.get(BLAH) 之类的常量?
简而言之,如何创建具有最小/最大值的 GregorianCalendar 实例?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该有效:
This should work:
我采纳了 joekutner 的建议并运行它:
其中显示了最小值和最大值,并且它们之间指示了如果您尝试移动到最小值之前的第二个会发生什么 - 您会环绕。
这是版本 1.6.0_17。
I took joekutner's suggestion and ran it with:
Which shows the minimum and maximum, and between them an indication of what happens if you try to move to the second before the minimum - you wrap around.
This was version 1.6.0_17.
您可以尝试调用 Calendar.getMinimum() 对于每种类型的字段(即年、月等),然后在相应的字段类型上设置这些最小值。这将为您提供最小日历。我不知道是否有更快的方法。
You can try to call Calendar.getMinimum() for each type of field (i.e. year, month, etc.) and then set those minimum values on corresponding field types. This would give you the minimum calendar. I don't know if there is a faster way to do that.
其他答案可能是正确的,但使用过时的类。
java.time
旧的日期时间类(java.util.Date/.Calendar 等)已被 java.time 框架内置于 Java 8 及更高版本。
java.time 类的灵感来自 Joda-Time,由 JSR 310,由 ThreeTen-Extra 项目扩展,向后移植到 Java 6 和 Java 6 7 由 ThreeTen-Backport 项目开发,并在 ThreeTenABP 项目。请参阅教程。
UTC 的时间线,分辨率为 纳秒,使用
即时
。给定一个 offset-from-UTC,使用OffsetDateTime
。对于时区(偏移量+异常规则),请使用ZonedDateTime
,但本质上没有定义了最小值/最大值,ZoneId< /代码>
。对于没有时间和时区的仅日期值,请使用
本地日期
。对于不含日期和时区的仅当天时间值,请使用本地时间
。对于没有时区的日期时间,请使用LocalDateTime
。以下都是预定义的常量。
Instant.MIN
=-1000000000-01-01T00:00Z
Instant.MAX
=1000000000-12-31T23:59:59.999999999Z
OffsetDateTime.MIN
=-999999999-01-01T00:00:00+18:00
OffsetDateTime.MAX
=+999999999-12-31T23:59:59.999999999-18:00
LocalDate.MIN
= <代码>-999999999-01-01
LocalDate.MAX
=+999999999-12-31
LocalTime.MIN
=00 :00
<代码>LocalTime.MAX =
23:59:59.999999999
LocalDateTime.MIN
=-999999999-01-01T00:00:00
LocalDateTime.MAX
=+999999999-12-31T23:59:59.999999999
Year.MIN_VALUE
=-999,999,999
Year.MAX_VALUE
=+999,999,999
ZoneOffset.MIN
=-18:00
(但实际上是-12:00
)ZoneOffset.MAX
=+18: 00
(但实际上是+14:00
)警告:谨慎使用这些值作为某种标志或特殊含义。许多其他软件库和数据库可能不支持这些极端值。
对于标志或特殊含义(例如非空“无可用值”),我建议选择任意时刻,但避免在时间上向后或向前达到如此极端的程度。也许Unix纪元参考日期,UTC 1970年的第一个时刻, 1970-01-01T00:00:00Z。在 Java 中定义为常量: Instant.EPOCH
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。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.time framework built into Java 8 and later.
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-Backport project, and adapted to Android in the ThreeTenABP project. See Tutorial.
For a moment on the timeline in UTC with a resolution of nanoseconds, use
Instant
. Given an offset-from-UTC, useOffsetDateTime
. For a time zone (offset + rules for anomalies), useZonedDateTime
, but by its nature has no defined min/max, nor doesZoneId
. For a date-only value without time-of-day and without time zone, useLocalDate
. For a time-of-day only value without date and without time zone, useLocalTime
. For date-time without time zone, useLocalDateTime
.The following are all pre-defined constants.
Instant.MIN
=-1000000000-01-01T00:00Z
Instant.MAX
=1000000000-12-31T23:59:59.999999999Z
OffsetDateTime.MIN
=-999999999-01-01T00:00:00+18:00
OffsetDateTime.MAX
=+999999999-12-31T23:59:59.999999999-18:00
LocalDate.MIN
=-999999999-01-01
LocalDate.MAX
=+999999999-12-31
LocalTime.MIN
=00:00
LocalTime.MAX
=23:59:59.999999999
LocalDateTime.MIN
=-999999999-01-01T00:00:00
LocalDateTime.MAX
=+999999999-12-31T23:59:59.999999999
Year.MIN_VALUE
=-999,999,999
Year.MAX_VALUE
=+999,999,999
ZoneOffset.MIN
=-18:00
(but-12:00
in practice)ZoneOffset.MAX
=+18:00
(but+14:00
in practice)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
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.