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 Julian calendar system, which defines every fourth year as leap. This implementation follows the leap year rule strictly, even for dates before 8 CE, where leap years were actually irregular. In the Julian calendar, year zero does not exist: 1 BCE is followed by 1 CE.

Although the Julian calendar did not exist before 45 BCE, this chronology assumes it did, thus it is proleptic. This implementation also fixes the start of the year at January 1.

JulianChronology is thread-safe and immutable.

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

    
Serialization lock
 
     private static final long serialVersionUID = -8731039522547897247L;
 
     private static final long MILLIS_PER_YEAR =
         (long) (365.25 * .);
 
     private static final long MILLIS_PER_MONTH =
         (long) (365.25 * . / 12);

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

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

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

    
Cache of zone to chronology arrays
 
     private static final Map<DateTimeZoneJulianChronology[]> cCache = new HashMap<DateTimeZoneJulianChronology[]>();
 
     static {
          = getInstance(.);
     }
 
     static int adjustYearForSet(int year) {
         if (year <= 0) {
             if (year == 0) {
                 throw new IllegalFieldValueException
                     (DateTimeFieldType.year(), Integer.valueOf(year), nullnull);
             }
             year++;
         }
         return year;
     }

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

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

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

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

    
Gets an instance of the JulianChronology 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 JulianChronology getInstance(DateTimeZone zone) {
        return getInstance(zone, 4);
    }

    
Gets an instance of the JulianChronology 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 JulianChronology getInstance(DateTimeZone zoneint minDaysInFirstWeek) {
        if (zone == null) {
            zone = DateTimeZone.getDefault();
        }
        JulianChronology chrono;
        synchronized () {
            JulianChronology[] chronos = .get(zone);
            if (chronos == null) {
                chronos = new JulianChronology[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 JulianChronology(nullnullminDaysInFirstWeek);
                } else {
                    chrono = getInstance(.minDaysInFirstWeek);
                    chrono = new JulianChronology
                        (ZonedChronology.getInstance(chronozone), nullminDaysInFirstWeek);
                }
                chronos[minDaysInFirstWeek - 1] = chrono;
            }
        }
        return chrono;
    }
    // Constructors and instance variables
    //-----------------------------------------------------------------------

    
Restricted constructor
    JulianChronology(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);
    }
    long getDateMidnightMillis(int yearint monthOfYearint dayOfMonth)
        throws IllegalArgumentException
    {
        return super.getDateMidnightMillis(adjustYearForSet(year), monthOfYeardayOfMonth);
    }
    boolean isLeapYear(int year) {
        return (year & 3) == 0;
    }
    long calculateFirstDayOfYearMillis(int year) {
        // Java epoch is 1970-01-01 Gregorian which is 1969-12-19 Julian.
        // Calculate relative to the nearest leap year and account for the
        // difference later.
        int relativeYear = year - 1968;
        int leapYears;
        if (relativeYear <= 0) {
            // Add 3 before shifting right since /4 and >>2 behave differently
            // on negative numbers.
            leapYears = (relativeYear + 3) >> 2;
        } else {
            leapYears = relativeYear >> 2;
            // For post 1968 an adjustment is needed as jan1st is before leap day
            if (!isLeapYear(year)) {
                leapYears++;
            }
        }
        
        long millis = (relativeYear * 365L + leapYears) * (long).;
        // Adjust to account for difference between 1968-01-01 and 1969-12-19.
        return millis - (366L + 352) * .;
    }
    int getMinYear() {
        return ;
    }
    int getMaxYear() {
        return ;
    }
    long getAverageMillisPerYear() {
        return ;
    }
        return  / 2;
    }
    long getAverageMillisPerMonth() {
        return ;
    }
        return (1969L *  + 352L * .) / 2;
    }
    protected void assemble(Fields fields) {
        if (getBase() == null) {
            super.assemble(fields);
            // Julian chronology has no year zero.
            fields.year = new SkipDateTimeField(thisfields.year);
            fields.weekyear = new SkipDateTimeField(thisfields.weekyear);
        }
    }
New to GrepCode? Check out our FAQ X