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 the Coptic calendar system, which defines every fourth year as leap, much like the Julian calendar. The year is broken down into 12 months, each 30 days in length. An extra period at the end of the year is either 5 or 6 days in length. In this implementation, it is considered a 13th month.

Year 1 in the Coptic calendar began on August 29, 284 CE (Julian), thus Coptic years do not begin at the same time as Julian years. This chronology is not proleptic, as it does not allow dates before the first Coptic year.

This implementation defines a day as midnight to midnight exactly as per the ISO chronology. Some references indicate that a coptic day starts at sunset on the previous ISO day, but this has not been confirmed and is not implemented.

CopticChronology is thread-safe and immutable.

Author(s):
Brian S O'Neill
Since:
1.0
See also:
Wikipedia
JulianChronology
 
 public final class CopticChronology extends BasicFixedMonthChronology {

    
Serialization lock
 
     private static final long serialVersionUID = -5972804258688333942L;

    
Constant value for 'Anno Martyrum' or 'Era of the Martyrs', equivalent to the value returned for AD/CE.
 
     public static final int AM = .;

    
A singleton era field.
 
     private static final DateTimeField ERA_FIELD = new BasicSingleEraDateTimeField("AM");

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

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

    
Cache of zone to chronology arrays
 
     private static final Map<DateTimeZoneCopticChronology[]> cCache = new HashMap<DateTimeZoneCopticChronology[]>();

    
Singleton instance of a UTC CopticChronology
 
     private static final CopticChronology INSTANCE_UTC;
     static {
         // init after static fields
          = getInstance(.);
     }
 
     //-----------------------------------------------------------------------
     
Gets an instance of the CopticChronology. The time zone of the returned instance is UTC.

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

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

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

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

    
Gets an instance of the CopticChronology 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 CopticChronology getInstance(DateTimeZone zoneint minDaysInFirstWeek) {
        if (zone == null) {
            zone = DateTimeZone.getDefault();
        }
        CopticChronology chrono;
        synchronized () {
            CopticChronology[] chronos = .get(zone);
            if (chronos == null) {
                chronos = new CopticChronology[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 == .) {
                    // First create without a lower limit.
                    chrono = new CopticChronology(nullnullminDaysInFirstWeek);
                    // Impose lower limit and make another CopticChronology.
                    DateTime lowerLimit = new DateTime(1, 1, 1, 0, 0, 0, 0, chrono);
                    chrono = new CopticChronology
                        (LimitChronology.getInstance(chronolowerLimitnull),
                         nullminDaysInFirstWeek);
                } else {
                    chrono = getInstance(.minDaysInFirstWeek);
                    chrono = new CopticChronology
                        (ZonedChronology.getInstance(chronozone), nullminDaysInFirstWeek);
                }
                chronos[minDaysInFirstWeek - 1] = chrono;
            }
        }
        return chrono;
    }
    // Constructors and instance variables
    //-----------------------------------------------------------------------
    
Restricted constructor.
    CopticChronology(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 calculateFirstDayOfYearMillis(int year) {
        // Java epoch is 1970-01-01 Gregorian which is 1686-04-23 Coptic.
        // Calculate relative to the nearest leap year and account for the
        // difference later.
        int relativeYear = year - 1687;
        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 1687 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 1687-01-01 and 1686-04-23.
        return millis + (365L - 112) * .;
    }
    //-----------------------------------------------------------------------
    int getMinYear() {
        return ;
    }
    //-----------------------------------------------------------------------
    int getMaxYear() {
        return ;
    }
    //-----------------------------------------------------------------------
        return (1686L *  + 112L * .) / 2;
    }
    //-----------------------------------------------------------------------
    protected void assemble(Fields fields) {
        if (getBase() == null) {
            super.assemble(fields);
            // Coptic, like Julian, has no year zero.
            fields.year = new SkipDateTimeField(thisfields.year);
            fields.weekyear = new SkipDateTimeField(thisfields.weekyear);
            
            fields.era = ;
            fields.monthOfYear = new BasicMonthOfYearDateTimeField(this, 13);
            fields.months = fields.monthOfYear.getDurationField();
        }
    }
New to GrepCode? Check out our FAQ X