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

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

Java: What/where are the maximum and minimum values of a GregorianCalendar?

javacalendargregorian-calendar

提问by WaelJ

What are the maximum and minimum values of a GregorianCalendar?

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

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

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

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

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

采纳答案by codefinger

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));

回答by pajton



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.

您可以尝试为每种类型的字段(即年、月等)调用Calendar.getMinimum(),然后在相应的字段类型上设置这些最小值。这将为您提供最少的日历。我不知道是否有更快的方法来做到这一点。

回答by martin clayton

I took joekutner's suggestion and ran it with:

我接受了 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

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.

这是版本 1.6.0_17。

回答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 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.

UTC时间线上,分辨率为纳秒,使用Instant. 给定一个offset-from-UTC,使用OffsetDateTime. 对于时区(偏移量 + 异常规则),使用ZonedDateTime,但本质上没有定义最小值/最大值,也没有ZoneId。对于没有时间和时区的仅日期值,请使用LocalDate. 对于没有日期和时区的仅时间值,请使用LocalTime。对于没有时区的日期时间,请使用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

对于标志或特殊含义,例如非空的“无可用值”,我建议选择任意时刻,但要避免在时间上向后或向前到达如此极端的范围。也许Unix 纪元参考日期,UTC 中的 1970 年的第一个时刻,1970-01-01T00:00:00Z。在 Java 中定义为常量:Instant.EPOCH



About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject 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.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多