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