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

What are the maximum and minimum values ​​of the GregorianCalendar?

Are they the same constants as Integer.MAX_VALUE or maybe GregorianCalendar.get (BLAH)?

In short, how can I create an instance of GregorianCalendar with a minimum value?

+3
java calendar gregorian-calendar
Mar 06 '10 at 15:39
source share
4 answers

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)); 
+6
Mar 6 '10 at 16:01
source share

I accepted joekutner's suggestion and ran it:

 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 

What shows the minimum and maximum, and between them - an indication of what will happen if you try to move to the second to a minimum - wrap.

It was version 1.6.0_17.

+5
Mar 06 '10 at 16:18
source share



You can try calling Calendar.getMinimum () for each field type (e.g. year, month, etc.), and then set these minimum values ​​for the corresponding field types. This will give you a minimal calendar. I do not know if there is a faster way to do this.

+4
Mar 06 '10 at 15:52
source share

Other answers may be correct, but use obsolete classes.

java.time

Old date and time classes (java.util.Date/.Calendar, etc.) have been superseded 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, ported to Java 6 and 7 by the ThreeTen-Backport project, and adapted for Android in the ThreeTenABP project. See the tutorial .

For a moment on the timeline in UTC with a nanosecond resolution, use Instant . Given the offset from UTC , use OffsetDateTime . For the time zone (offset + rules for anomalies), use ZonedDateTime , but min / max and ZoneId do not matter in ZoneId . For a date value only with no time and no time zone, use LocalDate . For a time of day with no date and no time zone, use LocalTime . For date and time without a time zone, use LocalDateTime .

All predefined constants are listed below.

Caution: be careful when using these values ​​as a kind of flag or special value. Many other software libraries and databases may not support these extreme values.

For a flag or special value, such as a non-zero β€œno value”, I suggest choosing an arbitrary moment, but I avoid going to such extreme achievements both backward and forward in time. Perhaps the date of reference to the Unix era , the first moment of 1970 at 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 supersede the nasty old obsolete time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises switching to the java.time classes.

To learn more, check out the Oracle tutorial . And search for qaru for many examples and explanations. The specification is JSR 310 .

You can exchange java.time objects directly with your database. Use a JDBC driver compatible with JDBC 4.2 or later. No strings needed, no java.sql.* Needed.

Where can I get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and others .

+1
Apr 07 '16 at 10:31 on
source share



All Articles