简体   繁体   English

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

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

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实例?

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

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。


You can try to call Calendar.getMinimum() for each type of field (ie year, month, etc.) and then set those minimum values on corresponding field types. 您可以尝试为每种类型的字段(即年,月等)调用Calendar.getMinimum() ,然后在相应的字段类型上设置这些最小值。 This would give you the minimum calendar. 这将为您提供最低日历。 I don't know if there is a faster way to do that. 我不知道是否有更快的方法可以做到这一点。

The other Answers may be correct but use outmoded classes. 其他答案可能是正确的,但使用过时的类。

java.time 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. 旧的日期时间类(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-Backport project, and adapted to Android in the ThreeTenABP project. 的java.time类由启发约达时间 ,由下式定义JSR 310 ,由ThreeTen-EXTRA项目扩展,由后移植到Java 6和7 ThreeTen-反向移植项目,并适合于在到Android ThreeTenABP项目。 See Tutorial . 请参阅教程

For a moment on the timeline in UTC with a resolution of nanoseconds , use Instant . UTC的时间轴上片刻,分辨率为纳秒 ,使用Instant Given an offset-from-UTC , use OffsetDateTime . 给定与UTC偏移量 ,使用OffsetDateTime For a time zone (offset + rules for anomalies), use ZonedDateTime , but by its nature has no defined min/max, nor does ZoneId . 对于时区 (偏移量+异常规则),使用ZonedDateTime ,但其性质没有定义的最小值/最大值,也没有ZoneId For a date-only value without time-of-day and without time zone, use LocalDate . 对于没有时间且没有时区的仅限日期的值,请使用LocalDate For a time-of-day only value without date and without time zone, use LocalTime . 对于没有日期且没有时区的仅限时间的值,请使用LocalTime For date-time without time zone, use LocalDateTime . 对于没有时区的日期时间,请使用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. 也许是Unix纪元参考日期 ,1970年的第一个时刻,UTC,1970-01-01T00:00:00Z。 Defined as a constant in Java: Instant.EPOCH 在Java中定义为常量: Instant.EPOCH


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM