Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 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;
 
 
Abstract implementation for calendar systems that use a typical day/month/year/leapYear model. Most of the utility methods required by subclasses are package-private, reflecting the intention that they be defined in the same package.

BasicChronology is thread-safe and immutable, and all subclasses must be as well.

Author(s):
Stephen Colebourne
Brian S O'Neill
Guy Allard
Since:
1.2, renamed from BaseGJChronology
 
 abstract class BasicChronology extends AssembledChronology {

    
Serialization lock
 
     private static final long serialVersionUID = 8283225332206808863L;
 
     private static final DurationField cMillisField;
     private static final DurationField cSecondsField;
     private static final DurationField cMinutesField;
     private static final DurationField cHoursField;
     private static final DurationField cHalfdaysField;
     private static final DurationField cDaysField;
     private static final DurationField cWeeksField;
 
     private static final DateTimeField cMillisOfSecondField;
     private static final DateTimeField cMillisOfDayField;
     private static final DateTimeField cSecondOfMinuteField;
     private static final DateTimeField cSecondOfDayField;
     private static final DateTimeField cMinuteOfHourField;
     private static final DateTimeField cMinuteOfDayField;
     private static final DateTimeField cHourOfDayField;
     private static final DateTimeField cHourOfHalfdayField;
     private static final DateTimeField cClockhourOfDayField;
     private static final DateTimeField cClockhourOfHalfdayField;
     private static final DateTimeField cHalfdayOfDayField;
 
     static {
          = new PreciseDurationField
             (DurationFieldType.seconds(), .);
          = new PreciseDurationField
             (DurationFieldType.minutes(), .);
          = new PreciseDurationField
             (DurationFieldType.hours(), .);
          = new PreciseDurationField
             (DurationFieldType.halfdays(), . / 2);
          = new PreciseDurationField
             (DurationFieldType.days(), .);
          = new PreciseDurationField
             (DurationFieldType.weeks(), .);
 
          = new PreciseDateTimeField
             (DateTimeFieldType.millisOfSecond(), );
 
          = new PreciseDateTimeField
             (DateTimeFieldType.millisOfDay(), );
              
          = new PreciseDateTimeField
             (DateTimeFieldType.secondOfMinute(), );
 
          = new PreciseDateTimeField
            (DateTimeFieldType.secondOfDay(), );
            (DateTimeFieldType.minuteOfHour(), );
            (DateTimeFieldType.minuteOfDay(), );
         = new PreciseDateTimeField
            (DateTimeFieldType.hourOfDay(), );
            (DateTimeFieldType.hourOfHalfday(), );
            (, DateTimeFieldType.clockhourOfDay());
            (, DateTimeFieldType.clockhourOfHalfday());
         = new HalfdayField();
    }
    private static final int CACHE_SIZE = 1 << 10;
    private static final int CACHE_MASK =  - 1;
    private transient final YearInfo[] iYearInfoCache = new YearInfo[];
    private final int iMinDaysInFirstWeek;
    BasicChronology(Chronology baseObject paramint minDaysInFirstWeek) {
        super(baseparam);
        if (minDaysInFirstWeek < 1 || minDaysInFirstWeek > 7) {
            throw new IllegalArgumentException
                ("Invalid min days in first week: " + minDaysInFirstWeek);
        }
         = minDaysInFirstWeek;
    }
    public DateTimeZone getZone() {
        Chronology base;
        if ((base = getBase()) != null) {
            return base.getZone();
        }
        return .;
    }
    public long getDateTimeMillis(
            int yearint monthOfYearint dayOfMonthint millisOfDay)
            throws IllegalArgumentException {
        Chronology base;
        if ((base = getBase()) != null) {
            return base.getDateTimeMillis(yearmonthOfYeardayOfMonthmillisOfDay);
        }
        FieldUtils.verifyValueBounds
            (DateTimeFieldType.millisOfDay(), millisOfDay, 0, .);
        return getDateMidnightMillis(yearmonthOfYeardayOfMonth) + millisOfDay;
    }
    public long getDateTimeMillis(
            int yearint monthOfYearint dayOfMonth,
            int hourOfDayint minuteOfHourint secondOfMinuteint millisOfSecond)
            throws IllegalArgumentException {
        Chronology base;
        if ((base = getBase()) != null) {
            return base.getDateTimeMillis(yearmonthOfYeardayOfMonth,
                                          hourOfDayminuteOfHoursecondOfMinutemillisOfSecond);
        }
        FieldUtils.verifyValueBounds(DateTimeFieldType.hourOfDay(), hourOfDay, 0, 23);
        FieldUtils.verifyValueBounds(DateTimeFieldType.minuteOfHour(), minuteOfHour, 0, 59);
        FieldUtils.verifyValueBounds(DateTimeFieldType.secondOfMinute(), secondOfMinute, 0, 59);
        FieldUtils.verifyValueBounds(DateTimeFieldType.millisOfSecond(), millisOfSecond, 0, 999);
        return getDateMidnightMillis(yearmonthOfYeardayOfMonth)
            + hourOfDay * .
            + minuteOfHour * .
            + secondOfMinute * .
            + millisOfSecond;
    }
    public int getMinimumDaysInFirstWeek() {
        return ;
    }

    
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) {
        return super.equals(obj);
    }

    
A suitable hash code for the chronology.

Returns:
the hash code
Since:
1.6
    public int hashCode() {
        return getClass().getName().hashCode() * 11 + getZone().hashCode() + getMinimumDaysInFirstWeek();
    }
    // Output
    //-----------------------------------------------------------------------
    
Gets a debugging toString.

Returns:
a debugging string
    public String toString() {
        StringBuffer sb = new StringBuffer(60);
        String name = getClass().getName();
        int index = name.lastIndexOf('.');
        if (index >= 0) {
            name = name.substring(index + 1);
        }
        sb.append(name);
        sb.append('[');
        DateTimeZone zone = getZone();
        if (zone != null) {
            sb.append(zone.getID());
        }
        if (getMinimumDaysInFirstWeek() != 4) {
            sb.append(",mdfw=");
            sb.append(getMinimumDaysInFirstWeek());
        }
        sb.append(']');
        return sb.toString();
    }
    protected void assemble(Fields fields) {
        // First copy fields that are the same for all Gregorian and Julian
        // chronologies.
        fields.millis = ;
        fields.seconds = ;
        fields.minutes = ;
        fields.hours = ;
        fields.halfdays = ;
        fields.days = ;
        fields.weeks = ;
        fields.millisOfSecond = ;
        fields.millisOfDay = ;
        fields.secondOfMinute = ;
        fields.secondOfDay = ;
        fields.minuteOfHour = ;
        fields.minuteOfDay = ;
        fields.hourOfDay = ;
        fields.hourOfHalfday = ;
        fields.clockhourOfDay = ;
        fields.clockhourOfHalfday = ;
        fields.halfdayOfDay = ;
        // Now create fields that have unique behavior for Gregorian and Julian
        // chronologies.
        fields.year = new BasicYearDateTimeField(this);
        fields.yearOfEra = new GJYearOfEraDateTimeField(fields.yearthis);
        // Define one-based centuryOfEra and yearOfCentury.
        DateTimeField field = new OffsetDateTimeField(
            fields.yearOfEra, 99);
        fields.centuryOfEra = new DividedDateTimeField(
            field, DateTimeFieldType.centuryOfEra(), 100);
        
        field = new RemainderDateTimeField(
            (DividedDateTimeFieldfields.centuryOfEra);
        fields.yearOfCentury = new OffsetDateTimeField(
            field, DateTimeFieldType.yearOfCentury(), 1);
        fields.era = new GJEraDateTimeField(this);
        fields.dayOfWeek = new GJDayOfWeekDateTimeField(thisfields.days);
        fields.dayOfMonth = new BasicDayOfMonthDateTimeField(thisfields.days);
        fields.dayOfYear = new BasicDayOfYearDateTimeField(thisfields.days);
        fields.monthOfYear = new GJMonthOfYearDateTimeField(this);
        fields.weekyear = new BasicWeekyearDateTimeField(this);
        fields.weekOfWeekyear = new BasicWeekOfWeekyearDateTimeField(thisfields.weeks);
        
        field = new RemainderDateTimeField(
            fields.weekyear, DateTimeFieldType.weekyearOfCentury(), 100);
        fields.weekyearOfCentury = new OffsetDateTimeField(
            field, DateTimeFieldType.weekyearOfCentury(), 1);
        
        // The remaining (imprecise) durations are available from the newly
        // created datetime fields.
        fields.years = fields.year.getDurationField();
        fields.centuries = fields.centuryOfEra.getDurationField();
        fields.months = fields.monthOfYear.getDurationField();
        fields.weekyears = fields.weekyear.getDurationField();
    }
    //-----------------------------------------------------------------------
    
Get the number of days in the year.

Returns:
366
    int getDaysInYearMax() {
        return 366;
    }

    
Get the number of days in the year.

Parameters:
year the year to use
Returns:
366 if a leap year, otherwise 365
    int getDaysInYear(int year) {
        return isLeapYear(year) ? 366 : 365;
    }

    
Get the number of weeks in the year.

Parameters:
year the year to use
Returns:
number of weeks in the year
    int getWeeksInYear(int year) {
        long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
        long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
        return (int) ((firstWeekMillis2 - firstWeekMillis1) / .);
    }

    
Get the millis for the first week of a year.

Parameters:
year the year to use
Returns:
millis
    long getFirstWeekOfYearMillis(int year) {
        long jan1millis = getYearMillis(year);
        int jan1dayOfWeek = getDayOfWeek(jan1millis);
        
        if (jan1dayOfWeek > (8 - )) {
            // First week is end of previous year because it doesn't have enough days.
            return jan1millis + (8 - jan1dayOfWeek)
                * (long).;
        } else {
            // First week is start of this year because it has enough days.
            return jan1millis - (jan1dayOfWeek - 1)
                * (long).;
        }
    }

    
Get the milliseconds for the start of a year.

Parameters:
year The year to use.
Returns:
millis from 1970-01-01T00:00:00Z
    long getYearMillis(int year) {
        return getYearInfo(year).;
    }

    
Get the milliseconds for the start of a month.

Parameters:
year The year to use.
month The month to use
Returns:
millis from 1970-01-01T00:00:00Z
    long getYearMonthMillis(int yearint month) {
        long millis = getYearMillis(year);
        millis += getTotalMillisByYearMonth(yearmonth);
        return millis;
    }

    
Get the milliseconds for a particular date.

Parameters:
year The year to use.
month The month to use
dayOfMonth The day of the month to use
Returns:
millis from 1970-01-01T00:00:00Z
    long getYearMonthDayMillis(int yearint monthint dayOfMonth) {
        long millis = getYearMillis(year);
        millis += getTotalMillisByYearMonth(yearmonth);
        return millis + (dayOfMonth - 1) * (long).;
    }
    
    

Parameters:
instant millis from 1970-01-01T00:00:00Z
    int getYear(long instant) {
        // Get an initial estimate of the year, and the millis value that
        // represents the start of that year. Then verify estimate and fix if
        // necessary.
        // Initial estimate uses values divided by two to avoid overflow.
        long unitMillis = getAverageMillisPerYearDividedByTwo();
        long i2 = (instant >> 1) + getApproxMillisAtEpochDividedByTwo();
        if (i2 < 0) {
            i2 = i2 - unitMillis + 1;
        }
        int year = (int) (i2 / unitMillis);
        long yearStart = getYearMillis(year);
        long diff = instant - yearStart;
        if (diff < 0) {
            year--;
        } else if (diff >= . * 365L) {
            // One year may need to be added to fix estimate.
            long oneYear;
            if (isLeapYear(year)) {
                oneYear = . * 366L;
            } else {
                oneYear = . * 365L;
            }
            yearStart += oneYear;
            if (yearStart <= instant) {
                // Didn't go too far, so actually add one year.
                year++;
            }
        }
        return year;
    }

    

Parameters:
millis from 1970-01-01T00:00:00Z
    int getMonthOfYear(long millis) {
        return getMonthOfYear(millisgetYear(millis));
    }

    

Parameters:
millis from 1970-01-01T00:00:00Z
year precalculated year of millis
    abstract int getMonthOfYear(long millisint year);

    

Parameters:
millis from 1970-01-01T00:00:00Z
    int getDayOfMonth(long millis) {
        int year = getYear(millis);
        int month = getMonthOfYear(millisyear);
        return getDayOfMonth(millisyearmonth);
    }

    

Parameters:
millis from 1970-01-01T00:00:00Z
year precalculated year of millis
    int getDayOfMonth(long millisint year) {
        int month = getMonthOfYear(millisyear);
        return getDayOfMonth(millisyearmonth);
    }

    

Parameters:
millis from 1970-01-01T00:00:00Z
year precalculated year of millis
month precalculated month of millis
    int getDayOfMonth(long millisint yearint month) {
        long dateMillis = getYearMillis(year);
        dateMillis += getTotalMillisByYearMonth(yearmonth);
        return (int) ((millis - dateMillis) / .) + 1;
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
    int getDayOfYear(long instant) {
        return getDayOfYear(instantgetYear(instant));
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
year precalculated year of millis
    int getDayOfYear(long instantint year) {
        long yearStart = getYearMillis(year);
        return (int) ((instant - yearStart) / .) + 1;
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
    int getWeekyear(long instant) {
        int year = getYear(instant);
        int week = getWeekOfWeekyear(instantyear);
        if (week == 1) {
            return getYear(instant + .);
        } else if (week > 51) {
            return getYear(instant - (2 * .));
        } else {
            return year;
        }
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
    int getWeekOfWeekyear(long instant) {
        return getWeekOfWeekyear(instantgetYear(instant));
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
year precalculated year of millis
    int getWeekOfWeekyear(long instantint year) {
        long firstWeekMillis1 = getFirstWeekOfYearMillis(year);
        if (instant < firstWeekMillis1) {
            return getWeeksInYear(year - 1);
        }
        long firstWeekMillis2 = getFirstWeekOfYearMillis(year + 1);
        if (instant >= firstWeekMillis2) {
            return 1;
        }
        return (int) ((instant - firstWeekMillis1) / .) + 1;
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
    int getDayOfWeek(long instant) {
        // 1970-01-01 is day of week 4, Thursday.
        long daysSince19700101;
        if (instant >= 0) {
            daysSince19700101 = instant / .;
        } else {
            daysSince19700101 = (instant - (. - 1))
                / .;
            if (daysSince19700101 < -3) {
                return 7 + (int) ((daysSince19700101 + 4) % 7);
            }
        }
        return 1 + (int) ((daysSince19700101 + 3) % 7);
    }

    

Parameters:
instant millis from 1970-01-01T00:00:00Z
    int getMillisOfDay(long instant) {
        if (instant >= 0) {
            return (int) (instant % .);
        } else {
            return (. - 1)
                + (int) ((instant + 1) % .);
        }
    }

    
Gets the maximum number of days in any month.

Returns:
31
    int getDaysInMonthMax() {
        return 31;
    }

    
Gets the maximum number of days in the month specified by the instant.

Parameters:
instant millis from 1970-01-01T00:00:00Z
Returns:
the maximum number of days in the month
    int getDaysInMonthMax(long instant) {
        int thisYear = getYear(instant);
        int thisMonth = getMonthOfYear(instantthisYear);
        return getDaysInYearMonth(thisYearthisMonth);
    }

    
Gets the maximum number of days in the month specified by the instant. The value represents what the user is trying to set, and can be used to optimise this method.

Parameters:
instant millis from 1970-01-01T00:00:00Z
value the value being set
Returns:
the maximum number of days in the month
    int getDaysInMonthMaxForSet(long instantint value) {
        return getDaysInMonthMax(instant);
    }
    //-----------------------------------------------------------------------
    
Gets the milliseconds for a date at midnight.

Parameters:
year the year
monthOfYear the month
dayOfMonth the day
Returns:
the milliseconds
    long getDateMidnightMillis(int yearint monthOfYearint dayOfMonth) {
        FieldUtils.verifyValueBounds(DateTimeFieldType.year(), yeargetMinYear(), getMaxYear());
        FieldUtils.verifyValueBounds(DateTimeFieldType.monthOfYear(), monthOfYear, 1, getMaxMonth(year));
        FieldUtils.verifyValueBounds(DateTimeFieldType.dayOfMonth(), dayOfMonth, 1, getDaysInYearMonth(yearmonthOfYear));
        return getYearMonthDayMillis(yearmonthOfYeardayOfMonth);
    }

    
Gets the difference between the two instants in years.

Parameters:
minuendInstant the first instant
subtrahendInstant the second instant
Returns:
the difference
    abstract long getYearDifference(long minuendInstantlong subtrahendInstant);

    
Is the specified year a leap year?

Parameters:
year the year to test
Returns:
true if leap
    abstract boolean isLeapYear(int year);

    
Gets the number of days in the specified month and year.

Parameters:
year the year
month the month
Returns:
the number of days
    abstract int getDaysInYearMonth(int yearint month);

    
Gets the maximum days in the specified month.

Parameters:
month the month
Returns:
the max days
    abstract int getDaysInMonthMax(int month);

    
Gets the total number of millis elapsed in this year at the start of the specified month, such as zero for month 1.

Parameters:
year the year
month the month
Returns:
the elapsed millis at the start of the month
    abstract long getTotalMillisByYearMonth(int yearint month);

    
Gets the millisecond value of the first day of the year.

Returns:
the milliseconds for the first of the year
    abstract long calculateFirstDayOfYearMillis(int year);

    
Gets the minimum supported year.

Returns:
the year
    abstract int getMinYear();

    
Gets the maximum supported year.

Returns:
the year
    abstract int getMaxYear();

    
Gets the maximum month for the specified year. This implementation calls getMaxMonth().

Parameters:
year the year
Returns:
the maximum month value
    int getMaxMonth(int year) {
        return getMaxMonth();
    }

    
Gets the maximum number of months.

Returns:
12
    int getMaxMonth() {
        return 12;
    }

    
Gets an average value for the milliseconds per year.

Returns:
the millis per year
    abstract long getAverageMillisPerYear();

    
Gets an average value for the milliseconds per year, divided by two.

Returns:
the millis per year divided by two
    abstract long getAverageMillisPerYearDividedByTwo();

    
Gets an average value for the milliseconds per month.

Returns:
the millis per month
    abstract long getAverageMillisPerMonth();

    
Returns a constant representing the approximate number of milliseconds elapsed from year 0 of this chronology, divided by two. This constant must be defined as:
    (yearAtEpoch * averageMillisPerYear + millisOfYearAtEpoch) / 2
 
where epoch is 1970-01-01 (Gregorian).
    abstract long getApproxMillisAtEpochDividedByTwo();

    
Sets the year from an instant and year.

Parameters:
instant millis from 1970-01-01T00:00:00Z
year the year to set
Returns:
the updated millis
    abstract long setYear(long instantint year);
    //-----------------------------------------------------------------------
    // Although accessed by multiple threads, this method doesn't need to be synchronized.
    private YearInfo getYearInfo(int year) {
        YearInfo info = [year & ];
        if (info == null || info.iYear != year) {
            info = new YearInfo(yearcalculateFirstDayOfYearMillis(year));
            [year & ] = info;
        }
        return info;
    }
    private static class HalfdayField extends PreciseDateTimeField {
        private static final long serialVersionUID = 581601443656929254L;
        HalfdayField() {
            super(DateTimeFieldType.halfdayOfDay(), );
        }
        public String getAsText(int fieldValueLocale locale) {
            return GJLocaleSymbols.forLocale(locale).halfdayValueToText(fieldValue);
        }
        public long set(long millisString textLocale locale) {
            return set(millis, GJLocaleSymbols.forLocale(locale).halfdayTextToValue(text));
        }
        public int getMaximumTextLength(Locale locale) {
            return GJLocaleSymbols.forLocale(locale).getHalfdayMaxTextLength();
        }
    }
    private static class YearInfo {
        public final int iYear;
        public final long iFirstDayMillis;
        YearInfo(int yearlong firstDayMillis) {
             = year;
             = firstDayMillis;
        }
    }
New to GrepCode? Check out our FAQ X