Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2013 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 chronology that follows the rules of the ISO8601 standard, which is compatible with Gregorian for all modern dates. When ISO does not define a field, but it can be determined (such as AM/PM) it is included.

With the exception of century related fields, ISOChronology is exactly the same as GregorianChronology. In this chronology, centuries and year of century are zero based. For all years, the century is determined by dropping the last two digits of the year, ignoring sign. The year of century is the value of the last two year digits.

ISOChronology is thread-safe and immutable.

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public final class ISOChronology extends AssembledChronology {
    
    
Serialization lock
 
     private static final long serialVersionUID = -6212696554273812441L;

    
Singleton instance of a UTC ISOChronology
 
     private static final ISOChronology INSTANCE_UTC;
         
     private static final int FAST_CACHE_SIZE = 64;

    
Fast cache of zone to chronology
 
     private static final ISOChronology[] cFastCache;

    
Cache of zone to chronology
 
     private static final Map<DateTimeZoneISOChronologycCache = new HashMap<DateTimeZoneISOChronology>();
     static {
          = new ISOChronology[];
          = new ISOChronology(GregorianChronology.getInstanceUTC());
         .put(.);
     }

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

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

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

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

    
Gets an instance of the ISOChronology 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 ISOChronology getInstance(DateTimeZone zone) {
         if (zone == null) {
             zone = DateTimeZone.getDefault();
         }
         int index = System.identityHashCode(zone) & ( - 1);
        ISOChronology chrono = [index];
        if (chrono != null && chrono.getZone() == zone) {
            return chrono;
        }
        synchronized () {
            chrono = .get(zone);
            if (chrono == null) {
                chrono = new ISOChronology(ZonedChronology.getInstance(zone));
                .put(zonechrono);
            }
        }
        [index] = chrono;
        return chrono;
    }
    // Constructors and instance variables
    //-----------------------------------------------------------------------

    
Restricted constructor
    private ISOChronology(Chronology base) {
        super(basenull);
    }
    // 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);
    }
    // Output
    //-----------------------------------------------------------------------
    
Gets a debugging toString.

Returns:
a debugging string
    public String toString() {
        String str = "ISOChronology";
        DateTimeZone zone = getZone();
        if (zone != null) {
            str = str + '[' + zone.getID() + ']';
        }
        return str;
    }
    protected void assemble(Fields fields) {
        if (getBase().getZone() == .) {
            // Use zero based century and year of century.
            fields.centuryOfEra = new DividedDateTimeField(
                ., DateTimeFieldType.centuryOfEra(), 100);
            fields.yearOfCentury = new RemainderDateTimeField(
                (DividedDateTimeFieldfields.centuryOfEra, DateTimeFieldType.yearOfCentury());
            fields.weekyearOfCentury = new RemainderDateTimeField(
                (DividedDateTimeFieldfields.centuryOfEra, DateTimeFieldType.weekyearOfCentury());
            fields.centuries = fields.centuryOfEra.getDurationField();
        }
    }
    //-----------------------------------------------------------------------
    
Checks if this chronology instance equals another.

Parameters:
obj the object to compare to
Returns:
true if equal
Since:
1.6
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof ISOChronology) {
            ISOChronology chrono = (ISOChronologyobj;
            return getZone().equals(chrono.getZone());
        }
        return false;
    }

    
A suitable hash code for the chronology.

Returns:
the hash code
Since:
1.6
    public int hashCode() {
        return "ISO".hashCode() * 11 + getZone().hashCode();
    }
    //-----------------------------------------------------------------------
    
Serialize ISOChronology instances using a small stub. This reduces the serialized size, and deserialized instances come from the cache.
    private Object writeReplace() {
        return new Stub(getZone());
    }
    private static final class Stub implements Serializable {
        private static final long serialVersionUID = -6212696554273812441L;
        private transient DateTimeZone iZone;
        Stub(DateTimeZone zone) {
             = zone;
        }
        private Object readResolve() {
            return ISOChronology.getInstance();
        }
        private void writeObject(ObjectOutputStream outthrows IOException {
            out.writeObject();
        }
        private void readObject(ObjectInputStream in)
            throws IOExceptionClassNotFoundException
        {
             = (DateTimeZone)in.readObject();
        }
    }
New to GrepCode? Check out our FAQ X