开发者

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

开发者 https://www.devze.com 2022-12-22 15:31 出处:网络
What are the maximum and minimum values of a GregorianCalendar? Are 开发者_Python百科they in a constant like Integer.MAX_VALUE, or maybe GregorianCalendar.get(BLAH)?

What are the maximum and minimum values of a GregorianCalendar?

Are 开发者_Python百科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?


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:

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.



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.


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.

  • 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?

  • Java SE 8, Java SE 9, Java SE 10, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消