Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.util
Class GregorianCalendar  view GregorianCalendar download GregorianCalendar.java

java.lang.Object
  extended byjava.util.Calendar
      extended byjava.util.GregorianCalendar
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class GregorianCalendar
extends Calendar

This class represents the Gregorian calendar, that is used in most countries all over the world. It does also handle the Julian calendar for dates smaller than the date of the change to the Gregorian calendar. The Gregorian calendar differs from the Julian calendar by a different leap year rule (no leap year every 100 years, except if year is divisible by 400).

This change date is different from country to country, and can be changed with setGregorianChange. The first countries to adopt the Gregorian calendar did so on the 15th of October, 1582. This date followed October the 4th, 1582 in the Julian calendar system. The non-existant days that were omitted when the change took place are interpreted as Gregorian dates.

Prior to the changeover date, New Year's Day occurred on the 25th of March. However, this class always takes New Year's Day as being the 1st of January. Client code should manually adapt the year value, if required, for dates between January the 1st and March the 24th in years prior to the changeover.

Any date infinitely forwards or backwards in time can be represented by this class. A proleptic calendar system is used, which allows future dates to be created via the existing rules. This allows meaningful and consistent dates to be produced for all years. However, dates are only historically accurate following March the 1st, 4AD when the Julian calendar system was adopted. Prior to this, leap year rules were applied erraticly.

There are two eras available for the Gregorian calendar, namely BC and AD.

Weeks are defined as a period of seven days, beginning on the first day of the week, as returned by getFirstDayOfWeek(), and ending on the day prior to this.

The weeks of the year are numbered from 1 to a possible 53. The first week of the year is defined as the first week that contains at least the minimum number of days of the first week in the new year (retrieved via getMinimalDaysInFirstWeek()). All weeks after this are numbered from 2 onwards.

For example, take the year 2004. It began on a Thursday. The first week of 2004 depends both on where a week begins and how long it must minimally last. Let's say that the week begins on a Monday and must have a minimum of 5 days. In this case, the first week begins on Monday, the 5th of January. The first 4 days (Thursday to Sunday) are not eligible, as they are too few to make up the minimum number of days of the first week which must be in the new year. If the minimum was lowered to 4 days, then the first week would instead begin on Monday, the 29th of December, 2003. This first week has 4 of its days in the new year, and is now eligible.

The weeks of the month are numbered from 0 to a possible 6. The first week of the month (numbered 1) is a set of days, prior to the first day of the week, which number at least the minimum number of days in a week. Unlike the first week of the year, the first week of the month only uses days from that particular month. As a consequence, it may have a variable number of days (from the minimum number required up to a full week of 7) and it need not start on the first day of the week. It must, however, be following by the first day of the week, as this marks the beginning of week 2. Any days of the month which occur prior to the first week (because the first day of the week occurs before the minimum number of days is met) are seen as week 0.

Again, we will take the example of the year 2004 to demonstrate this. September 2004 begins on a Wednesday. Taking our first day of the week as Monday, and the minimum length of the first week as 6, we find that week 1 runs from Monday, the 6th of September to Sunday the 12th. Prior to the 6th, there are only 5 days (Wednesday through to Sunday). This is too small a number to meet the minimum, so these are classed as being days in week 0. Week 2 begins on the 13th, and so on. This changes if we reduce the minimum to 5. In this case, week 1 is a truncated week from Wednesday the 1st to Sunday the 5th, and week 0 doesn't exist. The first seven day week is week 2, starting on the 6th.

On using the clear() method, the Gregorian calendar returns to its default value of the 1st of January, 1970 AD 00:00:00 (the epoch). The day of the week is set to the correct day for that particular time. The day is also the first of the month, and the date is in week 0.


Field Summary
static int AD
          Constant representing the era AD (Anno Domini).
static int BC
          Constant representing the era BC (Before Christ).
private static int EPOCH_DAYS
          Days in the epoch.
private  long gregorianCutover
          The point at which the Gregorian calendar rules were used.
private static int[] maximums
          The maximum values for the calendar fields.
private static int[] minimums
          The minimum values for the calendar fields.
(package private) static long serialVersionUID
          For compatability with Sun's JDK.
 
Fields inherited from class java.util.Calendar
AM, AM_PM, APRIL, areFieldsSet, AUGUST, DATE, DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_WEEK_IN_MONTH, DAY_OF_YEAR, DECEMBER, DST_OFFSET, ERA, FEBRUARY, FIELD_COUNT, fields, FRIDAY, HOUR, HOUR_OF_DAY, isSet, isTimeSet, JANUARY, JULY, JUNE, MARCH, MAY, MILLISECOND, MINUTE, MONDAY, MONTH, NOVEMBER, OCTOBER, PM, SATURDAY, SECOND, SEPTEMBER, SUNDAY, THURSDAY, time, TUESDAY, UNDECIMBER, WEDNESDAY, WEEK_OF_MONTH, WEEK_OF_YEAR, YEAR, ZONE_OFFSET
 
Constructor Summary
  GregorianCalendar()
          Constructs a new GregorianCalender representing the current time, using the default time zone and the default locale.
  GregorianCalendar(int year, int month, int day)
          Constructs a new GregorianCalendar representing midnight on the given date with the default time zone and locale.
  GregorianCalendar(int year, int month, int day, int hour, int minute)
          Constructs a new GregorianCalendar representing midnight on the given date with the default time zone and locale.
  GregorianCalendar(int year, int month, int day, int hour, int minute, int second)
          Constructs a new GregorianCalendar representing midnight on the given date with the default time zone and locale.
  GregorianCalendar(Locale locale)
          Constructs a new GregorianCalender representing the current time, using the default time zone and the specified locale.
  GregorianCalendar(TimeZone zone)
          Constructs a new GregorianCalender representing the current time, using the specified time zone and the default locale.
  GregorianCalendar(TimeZone zone, Locale locale)
          Constructs a new GregorianCalender representing the current time with the given time zone and the given locale.
private GregorianCalendar(TimeZone zone, Locale locale, boolean unused)
          Common constructor that all constructors should call.
 
Method Summary
 void add(int field, int amount)
          Adds the specified amount of time to the given time field.
private  void calculateDay(int[] fields, long day, boolean gregorian)
          Converts the given linear day into era, year, month, day_of_year, day_of_month, day_of_week, and writes the result into the fields array.
private  void cleanUpAfterRoll(int field, int delta)
          Checks that the fields are still within their legal bounds, following use of the roll() method.
protected  void computeFields()
          Converts the milliseconds since the epoch UTC (time) to time fields (fields).
protected  void computeTime()
          Converts the time field values (fields) to milliseconds since the epoch UTC (time).
 boolean equals(java.lang.Object o)
          Compares the given calendar with this.
 int getActualMaximum(int field)
          Gets the actual maximum value that is allowed for the specified field.
 int getActualMinimum(int field)
          Gets the actual minimum value that is allowed for the specified field.
private  int getFirstDayOfMonth(int year, int month)
          Returns the day of the week for the first day of a given month (0..11)
 int getGreatestMinimum(int field)
          Gets the greatest minimum value that is allowed for the specified field.
 Date getGregorianChange()
          Gets the date of the switch from Julian dates to Gregorian dates.
 int getLeastMaximum(int field)
          Gets the smallest maximum value that is allowed for the specified field.
private  long getLinearDay(int year, int dayOfYear, boolean gregorian)
          Get the linear day in days since the epoch, using the Julian or Gregorian calendar as specified.
 int getMaximum(int field)
          Gets the biggest value that is allowed for the specified field.
 int getMinimum(int field)
          Gets the smallest value that is allowed for the specified field.
private  int getWeekDay(int year, int dayOfYear)
          Retrieves the day of the week corresponding to the specified day of the specified year.
 int hashCode()
          Return a hash code for this object, following the general contract specified by Object.hashCode()>Object.hashCode() 55 .
private  boolean isGregorian(int year, int dayOfYear)
          Takes a year, and a (zero based) day of year and determines if it is gregorian or not.
 boolean isLeapYear(int year)
           Determines if the given year is a leap year.
private  void nonLeniencyCheck()
          Check set fields for validity, without leniency.
 void roll(int field, boolean up)
          Rolls the specified time field up or down.
 void roll(int field, int amount)
          Rolls the specified time field by the given amount.
 void setGregorianChange(Date date)
          Sets the date of the switch from Julian dates to Gregorian dates.
 
Methods inherited from class java.util.Calendar
after, before, clear, clear, clone, complete, get, getAvailableLocales, getFirstDayOfWeek, getInstance, getInstance, getInstance, getInstance, getMinimalDaysInFirstWeek, getTime, getTimeInMillis, getTimeZone, internalGet, isLenient, isSet, set, set, set, set, setFirstDayOfWeek, setLenient, setMinimalDaysInFirstWeek, setTime, setTimeInMillis, setTimeZone, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

BC

public static final int BC
Constant representing the era BC (Before Christ).

See Also:
Constant Field Values

AD

public static final int AD
Constant representing the era AD (Anno Domini).

See Also:
Constant Field Values

gregorianCutover

private long gregorianCutover
The point at which the Gregorian calendar rules were used. This may be changed by using setGregorianChange; The default is midnight (UTC) on October 5, 1582 (Julian), or October 15, 1582 (Gregorian).


serialVersionUID

static final long serialVersionUID
For compatability with Sun's JDK.

See Also:
Constant Field Values

EPOCH_DAYS

private static final int EPOCH_DAYS
Days in the epoch. Relative Jan 1, year '0' which is not a leap year. (although there is no year zero, this does not matter.) This is consistent with the formula: = (year-1)*365L + ((year-1) >> 2) Plus the gregorian correction: Math.floor((year-1) / 400.) - Math.floor((year-1) / 100.); For a correct julian date, the correction is -2 instead. The gregorian cutover in 1582 was 10 days, so by calculating the correction from year zero, we have 15 non-leap days (even centuries) minus 3 leap days (year 400,800,1200) = 12. Subtracting two corrects this to the correct number 10.

See Also:
Constant Field Values

minimums

private static final int[] minimums
The minimum values for the calendar fields.


maximums

private static final int[] maximums
The maximum values for the calendar fields.

Constructor Detail

GregorianCalendar

public GregorianCalendar()
Constructs a new GregorianCalender representing the current time, using the default time zone and the default locale.


GregorianCalendar

public GregorianCalendar(TimeZone zone)
Constructs a new GregorianCalender representing the current time, using the specified time zone and the default locale.


GregorianCalendar

public GregorianCalendar(Locale locale)
Constructs a new GregorianCalender representing the current time, using the default time zone and the specified locale.


GregorianCalendar

public GregorianCalendar(TimeZone zone,
                         Locale locale)
Constructs a new GregorianCalender representing the current time with the given time zone and the given locale.


GregorianCalendar

private GregorianCalendar(TimeZone zone,
                          Locale locale,
                          boolean unused)
Common constructor that all constructors should call.


GregorianCalendar

public GregorianCalendar(int year,
                         int month,
                         int day)
Constructs a new GregorianCalendar representing midnight on the given date with the default time zone and locale.


GregorianCalendar

public GregorianCalendar(int year,
                         int month,
                         int day,
                         int hour,
                         int minute)
Constructs a new GregorianCalendar representing midnight on the given date with the default time zone and locale.


GregorianCalendar

public GregorianCalendar(int year,
                         int month,
                         int day,
                         int hour,
                         int minute,
                         int second)
Constructs a new GregorianCalendar representing midnight on the given date with the default time zone and locale.

Method Detail

setGregorianChange

public void setGregorianChange(Date date)
Sets the date of the switch from Julian dates to Gregorian dates. You can use new Date(Long.MAX_VALUE) to use a pure Julian calendar, or Long.MIN_VALUE for a pure Gregorian calendar.


getGregorianChange

public final Date getGregorianChange()
Gets the date of the switch from Julian dates to Gregorian dates.


isLeapYear

public boolean isLeapYear(int year)

Determines if the given year is a leap year. The result is undefined if the Gregorian change took place in 1800, so that the end of February is skipped, and that year is specified. (well...).

To specify a year in the BC era, use a negative value calculated as 1 - y, where y is the required year in BC. So, 1 BC is 0, 2 BC is -1, 3 BC is -2, etc.


getWeekDay

private int getWeekDay(int year,
                       int dayOfYear)
Retrieves the day of the week corresponding to the specified day of the specified year.


getFirstDayOfMonth

private int getFirstDayOfMonth(int year,
                               int month)
Returns the day of the week for the first day of a given month (0..11)


isGregorian

private boolean isGregorian(int year,
                            int dayOfYear)
Takes a year, and a (zero based) day of year and determines if it is gregorian or not.


nonLeniencyCheck

private void nonLeniencyCheck()
                       throws java.lang.IllegalArgumentException
Check set fields for validity, without leniency.


computeTime

protected void computeTime()
Converts the time field values (fields) to milliseconds since the epoch UTC (time).

Specified by:
computeTime in class Calendar

getLinearDay

private long getLinearDay(int year,
                          int dayOfYear,
                          boolean gregorian)
Get the linear day in days since the epoch, using the Julian or Gregorian calendar as specified. If you specify a nonpositive year it is interpreted as BC as following: 0 is 1 BC, -1 is 2 BC and so on.


calculateDay

private void calculateDay(int[] fields,
                          long day,
                          boolean gregorian)
Converts the given linear day into era, year, month, day_of_year, day_of_month, day_of_week, and writes the result into the fields array.


computeFields

protected void computeFields()
Converts the milliseconds since the epoch UTC (time) to time fields (fields).

Specified by:
computeFields in class Calendar

hashCode

public int hashCode()
Return a hash code for this object, following the general contract specified by Object.hashCode()>Object.hashCode() 55 .

Overrides:
hashCode in class Calendar

equals

public boolean equals(java.lang.Object o)
Compares the given calendar with this. An object, o, is equivalent to this if it is also a GregorianCalendar with the same time since the epoch under the same conditions (same change date and same time zone).

Overrides:
equals in class Calendar

add

public void add(int field,
                int amount)
Adds the specified amount of time to the given time field. The amount may be negative to subtract the time. If the field overflows it does what you expect: Jan, 25 + 10 Days is Feb, 4.

Specified by:
add in class Calendar

roll

public void roll(int field,
                 boolean up)
Rolls the specified time field up or down. This means add one to the specified field, but don't change the other fields. If the maximum for this field is reached, start over with the minimum value. Note: There may be situation, where the other fields must be changed, e.g rolling the month on May, 31. The date June, 31 is automatically converted to July, 1. This requires lenient settings.

Specified by:
roll in class Calendar

cleanUpAfterRoll

private void cleanUpAfterRoll(int field,
                              int delta)
Checks that the fields are still within their legal bounds, following use of the roll() method.


roll

public void roll(int field,
                 int amount)
Rolls the specified time field by the given amount. This means add amount to the specified field, but don't change the other fields. If the maximum for this field is reached, start over with the minimum value and vice versa for negative amounts. Note: There may be situation, where the other fields must be changed, e.g rolling the month on May, 31. The date June, 31 is automatically corrected to June, 30.

Overrides:
roll in class Calendar

getMinimum

public int getMinimum(int field)
Gets the smallest value that is allowed for the specified field.

Specified by:
getMinimum in class Calendar

getMaximum

public int getMaximum(int field)
Gets the biggest value that is allowed for the specified field.

Specified by:
getMaximum in class Calendar

getGreatestMinimum

public int getGreatestMinimum(int field)
Gets the greatest minimum value that is allowed for the specified field. This is the largest value returned by the getActualMinimum(int) method.

Specified by:
getGreatestMinimum in class Calendar

getLeastMaximum

public int getLeastMaximum(int field)
Gets the smallest maximum value that is allowed for the specified field. This is the smallest value returned by the getActualMaximum(int). For example, this is 28 for DAY_OF_MONTH (as all months have at least 28 days).

Specified by:
getLeastMaximum in class Calendar
Since:
1.2

getActualMinimum

public int getActualMinimum(int field)
Gets the actual minimum value that is allowed for the specified field. This value is dependent on the values of the other fields. Note that this calls complete() if not enough fields are set. This can have ugly side effects. The value given depends on the current time used by this instance.

Overrides:
getActualMinimum in class Calendar
Since:
1.2

getActualMaximum

public int getActualMaximum(int field)
Gets the actual maximum value that is allowed for the specified field. This value is dependent on the values of the other fields. Note that this calls complete() if not enough fields are set. This can have ugly side effects. The value given depends on the current time used by this instance; thus, leap years have a maximum day of month value of 29, rather than 28.

Overrides:
getActualMaximum in class Calendar