3

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?

1

4 Answers 4

5

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));
7
  • 2
    Unfortunately your solution doesn't work. After mingc.setTime(new Date(Long.MIN_VALUE)) you get: migc.get(Calendar.YEAR) == 292269055 or sth like this. That doesn't sound like a min to me:-).
    – pajton
    Mar 6, 2010 at 16:34
  • 1
    Thank you =D I'm only using these values for comparison so I'm fine, but for others this may be worthy of mention. From setTime() documentation: Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get().
    – WaelJ
    Mar 6, 2010 at 16:35
  • 5
    pajton, check this: <code>migc.get(Calendar.ERA)</code> 0 means BC 1 means AD So year 292269055 corresponds to 292269055 BC
    – WaelJ
    Mar 6, 2010 at 16:37
  • Right. This is quite clever actually;-)
    – pajton
    Mar 6, 2010 at 16:50
  • Better use new Date(0) for minimum date. Jun 15, 2016 at 7:06
5

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.

0
3

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.

1
  • I don’t think it will give you the expected result (have not tried). There are dependencies between those fields.
    – Anonymous
    Dec 29, 2022 at 12:39
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.