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

4

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?

Fauman answered 6/3, 2010 at 15:39 Comment(1)
For new readers to this question I recommend you don’t use GregorianCalendar. It was a mostly failed attempt to make up for the problems with Date. Use ZonedDateTime from java.time, the modern Java date and time API.Sample
P
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));
Pol answered 6/3, 2010 at 16:1 Comment(7)
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:-).Emmett
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().Fauman
pajton, check this: <code>migc.get(Calendar.ERA)</code> 0 means BC 1 means AD So year 292269055 corresponds to 292269055 BCFauman
Right. This is quite clever actually;-)Emmett
Better use new Date(0) for minimum date.Potto
The reason i searched, is that the Date is deprecated, and yet we use Date to set Calendar? :|Videlicet
@Fauman i get positive value that had some truncated 0 at end for long min, and i checked js supported date time, i change from -9........L to -8.........L still got that error, it is positive and unsupported by JSVidelicet
J
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.

Jem answered 6/3, 2010 at 16:18 Comment(0)
E
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.

Emmett answered 6/3, 2010 at 15:52 Comment(1)
I don’t think it will give you the expected result (have not tried). There are dependencies between those fields.Sample
C
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.

Caput answered 7/4, 2016 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.