Java:GregorianCalendar 的最大值和最小值是什么/在哪里?

发布于 2024-08-23 16:01:36 字数 161 浏览 11 评论 0 原文

GregorianCalendar 的最大值和最小值是多少?

它们是像 Integer.MAX_VALUE 这样的常量,还是 GregorianCalendar.get(BLAH) 之类的常量?

简而言之,如何创建具有最小/最大值的 GregorianCalendar 实例?

What are the maximum and minimum values of a GregorianCalendar?

Are they in a constant like Integer.MAX_VALUE, or maybe GregorianCalendar.get(BLAH)?

In a nutshell, how can I create a GregorianCalendar instance with min/max value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

韶华倾负 2024-08-30 16:01:36

这应该有效:

GregorianCalendar maxgc = new GregorianCalendar();
maxgc.setTime(new Date(Long.MAX_VALUE));

GregorianCalendar mingc = new GregorianCalendar();
mingc.setTime(new Date(Long.MIN_VALUE));

This should work:

GregorianCalendar maxgc = new GregorianCalendar();
maxgc.setTime(new Date(Long.MAX_VALUE));

GregorianCalendar mingc = new GregorianCalendar();
mingc.setTime(new Date(Long.MIN_VALUE));
舂唻埖巳落 2024-08-30 16:01:36

我采纳了 joekutner 的建议并运行它:

GregorianCalendar gCal = new GregorianCalendar( );

gCal.setTime(new Date(Long.MIN_VALUE));
System.out.println( "Min Date is " + gCal.getTime() + " " + gCal.get(Calendar.ERA));

gCal.set( Calendar.SECOND, 3 );
System.out.println( "Min Date less 1 second is " + gCal.getTime() + " " + gCal.get(Calendar.ERA));

gCal.setTime(new Date(Long.MAX_VALUE));
System.out.println( "Max Date is " + gCal.getTime() + " " + gCal.get(Calendar.ERA));


Min Date is Sun Dec 02 16:47:04 GMT 292269055 0
Min Date less 1 second is Sun Aug 17 07:12:54 GMT 292278994 1
Max Date is Sun Aug 17 07:12:55 GMT 292278994 1

其中显示了最小值和最大值,并且它们之间指示了如果您尝试移动到最小值之前的第二个会发生什么 - 您会环绕。

这是版本 1.6.0_17。

I took joekutner's suggestion and ran it with:

GregorianCalendar gCal = new GregorianCalendar( );

gCal.setTime(new Date(Long.MIN_VALUE));
System.out.println( "Min Date is " + gCal.getTime() + " " + gCal.get(Calendar.ERA));

gCal.set( Calendar.SECOND, 3 );
System.out.println( "Min Date less 1 second is " + gCal.getTime() + " " + gCal.get(Calendar.ERA));

gCal.setTime(new Date(Long.MAX_VALUE));
System.out.println( "Max Date is " + gCal.getTime() + " " + gCal.get(Calendar.ERA));


Min Date is Sun Dec 02 16:47:04 GMT 292269055 0
Min Date less 1 second is Sun Aug 17 07:12:54 GMT 292278994 1
Max Date is Sun Aug 17 07:12:55 GMT 292278994 1

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.

坚持沉默 2024-08-30 16:01:36

您可以尝试调用 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.

第几種人 2024-08-30 16:01:36

其他答案可能是正确的,但使用过时的类。

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

以下都是预定义的常量。

警告:谨慎使用这些值作为某种标志或特殊含义。许多其他软件库和数据库可能不支持这些极端值。

对于标志或特殊含义(例如非空“无可用值”),我建议选择任意时刻,但避免在时间上向后或向前达到如此极端的程度。也许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, use OffsetDateTime. For a time zone (offset + rules for anomalies), use ZonedDateTime, but by its nature has no defined min/max, 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.

The following are all pre-defined constants.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文