Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2009 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.chrono;
 
 import java.util.Map;
 
Implements a pure proleptic Gregorian calendar system, which defines every fourth year as leap, unless the year is divisible by 100 and not by 400. This improves upon the Julian calendar leap year rule.

Although the Gregorian calendar did not exist before 1582 CE, this chronology assumes it did, thus it is proleptic. This implementation also fixes the start of the year at January 1, and defines the year zero.

GregorianChronology is thread-safe and immutable.

Author(s):
Guy Allard
Stephen Colebourne
Brian S O'Neill
Since:
1.0
See also:
Wikipedia
JulianChronology
GJChronology
 
 public final class GregorianChronology extends BasicGJChronology {

    
Serialization lock
 
     private static final long serialVersionUID = -861407383323710522L;
 
     private static final long MILLIS_PER_YEAR =
         (long) (365.2425 * .);
 
     private static final long MILLIS_PER_MONTH =
         (long) (365.2425 * . / 12);
 
     private static final int DAYS_0000_TO_1970 = 719527;

    
The lowest year that can be fully supported.
 
     private static final int MIN_YEAR = -292275054;

    
The highest year that can be fully supported.
 
     private static final int MAX_YEAR = 292278993;

    
Singleton instance of a UTC GregorianChronology
 
     private static final GregorianChronology INSTANCE_UTC;

    
Cache of zone to chronology arrays
 
     private static final Map<DateTimeZoneGregorianChronology[]> cCache = new HashMap<DateTimeZoneGregorianChronology[]>();
 
     static {
          = getInstance(.);
     }

    
Gets an instance of the GregorianChronology. The time zone of the returned instance is UTC.

Returns:
a singleton UTC instance of the chronology
 
     public static GregorianChronology getInstanceUTC() {
         return ;
     }

    
Gets an instance of the GregorianChronology in the default time zone.

Returns:
a chronology in the default time zone
 
     public static GregorianChronology getInstance() {
         return getInstance(DateTimeZone.getDefault(), 4);
     }

    
Gets an instance of the GregorianChronology in the given time zone.

Parameters:
zone the time zone to get the chronology in, null is default
Returns:
a chronology in the specified time zone
 
     public static GregorianChronology getInstance(DateTimeZone zone) {
        return getInstance(zone, 4);
    }

    
Gets an instance of the GregorianChronology in the given time zone.

Parameters:
zone the time zone to get the chronology in, null is default
minDaysInFirstWeek minimum number of days in first week of the year; default is 4
Returns:
a chronology in the specified time zone
    public static GregorianChronology getInstance(DateTimeZone zoneint minDaysInFirstWeek) {
        if (zone == null) {
            zone = DateTimeZone.getDefault();
        }
        GregorianChronology chrono;
        synchronized () {
            GregorianChronology[] chronos = .get(zone);
            if (chronos == null) {
                chronos = new GregorianChronology[7];
                .put(zonechronos);
            }
            try {
                chrono = chronos[minDaysInFirstWeek - 1];
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new IllegalArgumentException
                    ("Invalid min days in first week: " + minDaysInFirstWeek);
            }
            if (chrono == null) {
                if (zone == .) {
                    chrono = new GregorianChronology(nullnullminDaysInFirstWeek);
                } else {
                    chrono = getInstance(.minDaysInFirstWeek);
                    chrono = new GregorianChronology
                        (ZonedChronology.getInstance(chronozone), nullminDaysInFirstWeek);
                }
                chronos[minDaysInFirstWeek - 1] = chrono;
            }
        }
        return chrono;
    }
    // Constructors and instance variables
    //-----------------------------------------------------------------------

    
Restricted constructor
    private GregorianChronology(Chronology baseObject paramint minDaysInFirstWeek) {
        super(baseparamminDaysInFirstWeek);
    }

    
Serialization singleton
    private Object readResolve() {
        Chronology base = getBase();
        int minDays = getMinimumDaysInFirstWeek();
        minDays = (minDays == 0 ? 4 : minDays);  // handle rename of BaseGJChronology
        return base == null ?
                getInstance(.minDays) :
                    getInstance(base.getZone(), minDays);
    }
    // Conversion
    //-----------------------------------------------------------------------
    
Gets the Chronology in the UTC time zone.

Returns:
the chronology in UTC
    public Chronology withUTC() {
        return ;
    }

    
Gets the Chronology in a specific time zone.

Parameters:
zone the zone to get the chronology in, null is default
Returns:
the chronology
    public Chronology withZone(DateTimeZone zone) {
        if (zone == null) {
            zone = DateTimeZone.getDefault();
        }
        if (zone == getZone()) {
            return this;
        }
        return getInstance(zone);
    }
    protected void assemble(Fields fields) {
        if (getBase() == null) {
            super.assemble(fields);
        }
    }
    boolean isLeapYear(int year) {
        return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
    }
    long calculateFirstDayOfYearMillis(int year) {
        // Initial value is just temporary.
        int leapYears = year / 100;
        if (year < 0) {
            // Add 3 before shifting right since /4 and >>2 behave differently
            // on negative numbers. When the expression is written as
            // (year / 4) - (year / 100) + (year / 400),
            // it works for both positive and negative values, except this optimization
            // eliminates two divisions.
            leapYears = ((year + 3) >> 2) - leapYears + ((leapYears + 3) >> 2) - 1;
        } else {
            leapYears = (year >> 2) - leapYears + (leapYears >> 2);
            if (isLeapYear(year)) {
                leapYears--;
            }
        }
        return (year * 365L + (leapYears - )) * .;
    }
    int getMinYear() {
        return ;
    }
    int getMaxYear() {
        return ;
    }
    long getAverageMillisPerYear() {
        return ;
    }
        return  / 2;
    }
    long getAverageMillisPerMonth() {
        return ;
    }
        return (1970L * ) / 2;
    }
New to GrepCode? Check out our FAQ X