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;
 
 
BaseChronology provides a skeleton implementation for chronology classes. Many utility methods are defined, but all fields are unsupported.

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

Author(s):
Brian S O'Neill
Since:
1.0
 
 public abstract class BaseChronology
         extends Chronology
         implements Serializable {

    
Serialization version.
 
     private static final long serialVersionUID = -7310865996721419676L;

    
Restricted constructor.
 
     protected BaseChronology() {
         super();
     }

    
Returns the DateTimeZone that this Chronology operates in, or null if unspecified.

Returns:
DateTimeZone null if unspecified
 
     public abstract DateTimeZone getZone();

    
Returns an instance of this Chronology that operates in the UTC time zone. Chronologies that do not operate in a time zone or are already UTC must return themself.

Returns:
a version of this chronology that ignores time zones
 
     public abstract Chronology withUTC();
    
    
Returns an instance of this Chronology that operates in any time zone.

Parameters:
zone to use, or default if null
Returns:
a version of this chronology with a specific time zone
See also:
ZonedChronology
 
     public abstract Chronology withZone(DateTimeZone zone);

    
Returns a datetime millisecond instant, formed from the given year, month, day, and millisecond values. The set of given values must refer to a valid datetime, or else an IllegalArgumentException is thrown.

The default implementation calls upon separate DateTimeFields to determine the result. Subclasses are encouraged to provide a more efficient implementation.

Parameters:
year year to use
monthOfYear month to use
dayOfMonth day of month to use
millisOfDay millisecond to use
Returns:
millisecond instant from 1970-01-01T00:00:00Z
 
     public long getDateTimeMillis(int yearint monthOfYearint dayOfMonth,
                                   int millisOfDay)
        throws IllegalArgumentException
    {
        long instant = year().set(0, year);
        instant = monthOfYear().set(instantmonthOfYear);
        instant = dayOfMonth().set(instantdayOfMonth);
        return millisOfDay().set(instantmillisOfDay);
    }

    
Returns a datetime millisecond instant, formed from the given year, month, day, hour, minute, second, and millisecond values. The set of given values must refer to a valid datetime, or else an IllegalArgumentException is thrown.

The default implementation calls upon separate DateTimeFields to determine the result. Subclasses are encouraged to provide a more efficient implementation.

Parameters:
year year to use
monthOfYear month to use
dayOfMonth day of month to use
hourOfDay hour to use
minuteOfHour minute to use
secondOfMinute second to use
millisOfSecond millisecond to use
Returns:
millisecond instant from 1970-01-01T00:00:00Z
    public long getDateTimeMillis(int yearint monthOfYearint dayOfMonth,
                                  int hourOfDayint minuteOfHour,
                                  int secondOfMinuteint millisOfSecond)
        throws IllegalArgumentException
    {
        long instant = year().set(0, year);
        instant = monthOfYear().set(instantmonthOfYear);
        instant = dayOfMonth().set(instantdayOfMonth);
        instant = hourOfDay().set(instanthourOfDay);
        instant = minuteOfHour().set(instantminuteOfHour);
        instant = secondOfMinute().set(instantsecondOfMinute);
        return millisOfSecond().set(instantmillisOfSecond);
    }

    
Returns a datetime millisecond instant, from from the given instant, hour, minute, second, and millisecond values. The set of given values must refer to a valid datetime, or else an IllegalArgumentException is thrown.

The default implementation calls upon separate DateTimeFields to determine the result. Subclasses are encouraged to provide a more efficient implementation.

Parameters:
instant instant to start from
hourOfDay hour to use
minuteOfHour minute to use
secondOfMinute second to use
millisOfSecond millisecond to use
Returns:
millisecond instant from 1970-01-01T00:00:00Z
    public long getDateTimeMillis(long instant,
                                  int hourOfDayint minuteOfHour,
                                  int secondOfMinuteint millisOfSecond)
        throws IllegalArgumentException
    {
        instant = hourOfDay().set(instanthourOfDay);
        instant = minuteOfHour().set(instantminuteOfHour);
        instant = secondOfMinute().set(instantsecondOfMinute);
        return millisOfSecond().set(instantmillisOfSecond);
    }
    //-----------------------------------------------------------------------
    
Validates whether the fields stored in a partial instant are valid.

This implementation uses org.joda.time.DateTimeField.getMinimumValue(org.joda.time.ReadablePartial,int[]) and org.joda.time.DateTimeField.getMaximumValue(org.joda.time.ReadablePartial,int[]).

Parameters:
partial the partial instant to validate
values the values to validate, not null
Throws:
java.lang.IllegalArgumentException if the instant is invalid
    public void validate(ReadablePartial partialint[] values) {
        // check values in standard range, catching really stupid cases like -1
        // this means that the second check will not hit trouble
        int size = partial.size();
        for (int i = 0; i < sizei++) {
            int value = values[i];
            DateTimeField field = partial.getField(i);
            if (value < field.getMinimumValue()) {
                throw new IllegalFieldValueException
                    (field.getType(), Integer.valueOf(value),
                     Integer.valueOf(field.getMinimumValue()), null);
            }
            if (value > field.getMaximumValue()) {
                throw new IllegalFieldValueException
                    (field.getType(), Integer.valueOf(value),
                     null, Integer.valueOf(field.getMaximumValue()));
            }
        }
        // check values in specific range, catching really odd cases like 30th Feb
        for (int i = 0; i < sizei++) {
            int value = values[i];
            DateTimeField field = partial.getField(i);
            if (value < field.getMinimumValue(partialvalues)) {
                throw new IllegalFieldValueException
                    (field.getType(), Integer.valueOf(value),
                     Integer.valueOf(field.getMinimumValue(partialvalues)), null);
            }
            if (value > field.getMaximumValue(partialvalues)) {
                throw new IllegalFieldValueException
                    (field.getType(), Integer.valueOf(value),
                     null, Integer.valueOf(field.getMaximumValue(partialvalues)));
            }
        }
    }

    
Gets the values of a partial from an instant.

Parameters:
partial the partial instant to use
instant the instant to query
Returns:
the values of the partial extracted from the instant
    public int[] get(ReadablePartial partiallong instant) {
        int size = partial.size();
        int[] values = new int[size];
        for (int i = 0; i < sizei++) {
            values[i] = partial.getFieldType(i).getField(this).get(instant);
        }
        return values;
    }

    
Sets the partial into the instant.

Parameters:
partial the partial instant to use
instant the instant to update
Returns:
the updated instant
    public long set(ReadablePartial partiallong instant) {
        for (int i = 0, isize = partial.size(); i < isizei++) {
            instant = partial.getFieldType(i).getField(this).set(instantpartial.getValue(i));
        }
        return instant;
    }
    //-----------------------------------------------------------------------
    
Gets the values of a period from an interval.

Parameters:
period the period instant to use
startInstant the start instant of an interval to query
endInstant the start instant of an interval to query
Returns:
the values of the period extracted from the interval
    public int[] get(ReadablePeriod periodlong startInstantlong endInstant) {
        int size = period.size();
        int[] values = new int[size];
        if (startInstant != endInstant) {
            for (int i = 0; i < sizei++) {
                DurationField field = period.getFieldType(i).getField(this);
                int value = field.getDifference(endInstantstartInstant);
                startInstant = field.add(startInstantvalue);
                values[i] = value;
            }
        }
        return values;
    }

    
Gets the values of a period from an interval.

Parameters:
period the period instant to use
duration the duration to query
Returns:
the values of the period extracted from the duration
    public int[] get(ReadablePeriod periodlong duration) {
        int size = period.size();
        int[] values = new int[size];
        if (duration != 0) {
            long current = 0;
            for (int i = 0; i < sizei++) {
                DurationField field = period.getFieldType(i).getField(this);
                if (field.isPrecise()) {
                    int value = field.getDifference(durationcurrent);
                    current = field.add(currentvalue);
                    values[i] = value;
                }
            }
        }
        return values;
    }

    
Adds the period to the instant, specifying the number of times to add.

Parameters:
period the period to add, null means add nothing
instant the instant to add to
scalar the number of times to add
Returns:
the updated instant
    public long add(ReadablePeriod periodlong instantint scalar) {
        if (scalar != 0 && period != null) {
            for (int i = 0, isize = period.size(); i < isizei++) {
                long value = period.getValue(i); // use long to allow for multiplication (fits OK)
                if (value != 0) {
                    instant = period.getFieldType(i).getField(this).add(instantvalue * scalar);
                }
            }
        }
        return instant;
    }
    //-----------------------------------------------------------------------
    
Adds the duration to the instant, specifying the number of times to add.

Parameters:
instant the instant to add to
duration the duration to add
scalar the number of times to add
Returns:
the updated instant
    public long add(long instantlong durationint scalar) {
        if (duration == 0 || scalar == 0) {
            return instant;
        }
        long add = FieldUtils.safeMultiply(durationscalar);
        return FieldUtils.safeAdd(instantadd);
    }
    // Millis
    //-----------------------------------------------------------------------
    
Get the millis duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField millis() {
        return UnsupportedDurationField.getInstance(DurationFieldType.millis());
    }

    
Get the millis of second field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField millisOfSecond() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.millisOfSecond(), millis());
    }

    
Get the millis of day field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField millisOfDay() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.millisOfDay(), millis());
    }
    // Second
    //-----------------------------------------------------------------------
    
Get the seconds duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField seconds() {
        return UnsupportedDurationField.getInstance(DurationFieldType.seconds());
    }

    
Get the second of minute field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField secondOfMinute() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.secondOfMinute(), seconds());
    }

    
Get the second of day field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField secondOfDay() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.secondOfDay(), seconds());
    }
    // Minute
    //-----------------------------------------------------------------------
    
Get the minutes duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField minutes() {
        return UnsupportedDurationField.getInstance(DurationFieldType.minutes());
    }

    
Get the minute of hour field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField minuteOfHour() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.minuteOfHour(), minutes());
    }

    
Get the minute of day field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField minuteOfDay() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.minuteOfDay(), minutes());
    }
    // Hour
    //-----------------------------------------------------------------------
    
Get the hours duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField hours() {
        return UnsupportedDurationField.getInstance(DurationFieldType.hours());
    }

    
Get the hour of day (0-23) field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField hourOfDay() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.hourOfDay(), hours());
    }

    
Get the hour of day (offset to 1-24) field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField clockhourOfDay() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.clockhourOfDay(), hours());
    }
    // Halfday
    //-----------------------------------------------------------------------
    
Get the halfdays duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField halfdays() {
        return UnsupportedDurationField.getInstance(DurationFieldType.halfdays());
    }

    
Get the hour of am/pm (0-11) field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField hourOfHalfday() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.hourOfHalfday(), hours());
    }

    
Get the hour of am/pm (offset to 1-12) field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField clockhourOfHalfday() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.clockhourOfHalfday(), hours());
    }

    
Get the AM(0) PM(1) field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField halfdayOfDay() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.halfdayOfDay(), halfdays());
    }
    // Day
    //-----------------------------------------------------------------------
    
Get the days duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField days() {
        return UnsupportedDurationField.getInstance(DurationFieldType.days());
    }

    
Get the day of week field for this chronology.

DayOfWeek values are defined in DateTimeConstants. They use the ISO definitions, where 1 is Monday and 7 is Sunday.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField dayOfWeek() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.dayOfWeek(), days());
    }

    
Get the day of month field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField dayOfMonth() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.dayOfMonth(), days());
    }

    
Get the day of year field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField dayOfYear() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.dayOfYear(), days());
    }
    // Week
    //-----------------------------------------------------------------------
    
Get the weeks duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField weeks() {
        return UnsupportedDurationField.getInstance(DurationFieldType.weeks());
    }

    
Get the week of a week based year field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField weekOfWeekyear() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.weekOfWeekyear(), weeks());
    }
    // Weekyear
    //-----------------------------------------------------------------------
    
Get the weekyears duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField weekyears() {
        return UnsupportedDurationField.getInstance(DurationFieldType.weekyears());
    }

    
Get the year of a week based year field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField weekyear() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.weekyear(), weekyears());
    }

    
Get the year of a week based year in a century field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField weekyearOfCentury() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.weekyearOfCentury(), weekyears());
    }
    // Month
    //-----------------------------------------------------------------------
    
Get the months duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField months() {
        return UnsupportedDurationField.getInstance(DurationFieldType.months());
    }

    
Get the month of year field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField monthOfYear() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.monthOfYear(), months());
    }
    // Year
    //-----------------------------------------------------------------------
    
Get the years duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField years() {
        return UnsupportedDurationField.getInstance(DurationFieldType.years());
    }

    
Get the year field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField year() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.year(), years());
    }

    
Get the year of era field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField yearOfEra() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.yearOfEra(), years());
    }

    
Get the year of century field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField yearOfCentury() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.yearOfCentury(), years());
    }
    // Century
    //-----------------------------------------------------------------------
    
Get the centuries duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField centuries() {
        return UnsupportedDurationField.getInstance(DurationFieldType.centuries());
    }

    
Get the century of era field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField centuryOfEra() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.centuryOfEra(), centuries());
    }
    // Era
    //-----------------------------------------------------------------------
    
Get the eras duration field for this chronology.

Returns:
DurationField or UnsupportedDurationField if unsupported
    public DurationField eras() {
        return UnsupportedDurationField.getInstance(DurationFieldType.eras());
    }

    
Get the era field for this chronology.

Returns:
DateTimeField or UnsupportedDateTimeField if unsupported
    public DateTimeField era() {
        return UnsupportedDateTimeField.getInstance(DateTimeFieldType.era(), eras());
    }
    //-----------------------------------------------------------------------
    
Gets a debugging toString.

Returns:
a debugging string
    public abstract String toString();
New to GrepCode? Check out our FAQ X