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;
 
Identifies a field, such as year or minuteOfHour, in a chronology-neutral way.

A field type defines the type of the field, such as hourOfDay. If does not directly enable any calculations, however it does provide a getField(org.joda.time.Chronology) method that returns the actual calculation engine for a particular chronology. It also provides access to the related DurationFieldTypes.

Instances of DateTimeFieldType are singletons. They can be compared using ==.

If required, you can create your own field, for example a quarterOfYear. You must create a subclass of DateTimeFieldType that defines the field type. This class returns the actual calculation engine from getField(org.joda.time.Chronology). The subclass should implement equals and hashCode.

Author(s):
Stephen Colebourne
Brian S O'Neill
Since:
1.0
 
 public abstract class DateTimeFieldType implements Serializable {

    
Serialization version
 
     private static final long serialVersionUID = -42615285973990L;

    
Ordinal values for standard field types.
 
     static final byte
         ERA = 1,
         YEAR_OF_ERA = 2,
         CENTURY_OF_ERA = 3,
         YEAR_OF_CENTURY = 4,
         YEAR = 5,
         DAY_OF_YEAR = 6,
         MONTH_OF_YEAR = 7,
         DAY_OF_MONTH = 8,
         WEEKYEAR_OF_CENTURY = 9,
         WEEKYEAR = 10,
         WEEK_OF_WEEKYEAR = 11,
         DAY_OF_WEEK = 12,
         HALFDAY_OF_DAY = 13,
         HOUR_OF_HALFDAY = 14,
         CLOCKHOUR_OF_HALFDAY = 15,
         CLOCKHOUR_OF_DAY = 16,
         HOUR_OF_DAY = 17,
         MINUTE_OF_DAY = 18,
         MINUTE_OF_HOUR = 19,
         SECOND_OF_DAY = 20,
         SECOND_OF_MINUTE = 21,
         MILLIS_OF_DAY = 22,
         MILLIS_OF_SECOND = 23;

    
The era field type.
 
     private static final DateTimeFieldType ERA_TYPE = new StandardDateTimeFieldType(
         "era", DurationFieldType.eras(), null);
    
The yearOfEra field type.
 
     private static final DateTimeFieldType YEAR_OF_ERA_TYPE = new StandardDateTimeFieldType(
         "yearOfEra", DurationFieldType.years(), DurationFieldType.eras());
    
The centuryOfEra field type.
 
     private static final DateTimeFieldType CENTURY_OF_ERA_TYPE = new StandardDateTimeFieldType(
         "centuryOfEra", DurationFieldType.centuries(), DurationFieldType.eras());
    
The yearOfCentury field type.
 
     private static final DateTimeFieldType YEAR_OF_CENTURY_TYPE = new StandardDateTimeFieldType(
         "yearOfCentury", DurationFieldType.years(), DurationFieldType.centuries());
    
The year field type.
 
     private static final DateTimeFieldType YEAR_TYPE = new StandardDateTimeFieldType(
         "year", DurationFieldType.years(), null);
    
The dayOfYear field type.
 
     private static final DateTimeFieldType DAY_OF_YEAR_TYPE = new StandardDateTimeFieldType(
         "dayOfYear", DurationFieldType.days(), DurationFieldType.years());
    
The monthOfYear field type.
 
     private static final DateTimeFieldType MONTH_OF_YEAR_TYPE = new StandardDateTimeFieldType(
         "monthOfYear", DurationFieldType.months(), DurationFieldType.years());
    
The dayOfMonth field type.
 
     private static final DateTimeFieldType DAY_OF_MONTH_TYPE = new StandardDateTimeFieldType(
         "dayOfMonth", DurationFieldType.days(), DurationFieldType.months());
    
The weekyearOfCentury field type.
 
     private static final DateTimeFieldType WEEKYEAR_OF_CENTURY_TYPE = new StandardDateTimeFieldType(
         "weekyearOfCentury", DurationFieldType.weekyears(), DurationFieldType.centuries());
    
The weekyear field type.
 
    private static final DateTimeFieldType WEEKYEAR_TYPE = new StandardDateTimeFieldType(
        "weekyear", DurationFieldType.weekyears(), null);
    
The weekOfWeekyear field type.
    private static final DateTimeFieldType WEEK_OF_WEEKYEAR_TYPE = new StandardDateTimeFieldType(
        "weekOfWeekyear", DurationFieldType.weeks(), DurationFieldType.weekyears());
    
The dayOfWeek field type.
    private static final DateTimeFieldType DAY_OF_WEEK_TYPE = new StandardDateTimeFieldType(
        "dayOfWeek", DurationFieldType.days(), DurationFieldType.weeks());

    
The halfday field type.
    private static final DateTimeFieldType HALFDAY_OF_DAY_TYPE = new StandardDateTimeFieldType(
        "halfdayOfDay", DurationFieldType.halfdays(), DurationFieldType.days());
    
The hourOfHalfday field type.
    private static final DateTimeFieldType HOUR_OF_HALFDAY_TYPE = new StandardDateTimeFieldType(
        "hourOfHalfday", DurationFieldType.hours(), DurationFieldType.halfdays());
    
The clockhourOfHalfday field type.
        "clockhourOfHalfday", DurationFieldType.hours(), DurationFieldType.halfdays());
    
The clockhourOfDay field type.
    private static final DateTimeFieldType CLOCKHOUR_OF_DAY_TYPE = new StandardDateTimeFieldType(
        "clockhourOfDay", DurationFieldType.hours(), DurationFieldType.days());
    
The hourOfDay field type.
    private static final DateTimeFieldType HOUR_OF_DAY_TYPE = new StandardDateTimeFieldType(
        "hourOfDay", DurationFieldType.hours(), DurationFieldType.days());
    
The minuteOfDay field type.
    private static final DateTimeFieldType MINUTE_OF_DAY_TYPE = new StandardDateTimeFieldType(
        "minuteOfDay", DurationFieldType.minutes(), DurationFieldType.days());
    
The minuteOfHour field type.
    private static final DateTimeFieldType MINUTE_OF_HOUR_TYPE = new StandardDateTimeFieldType(
        "minuteOfHour", DurationFieldType.minutes(), DurationFieldType.hours());
    
The secondOfDay field type.
    private static final DateTimeFieldType SECOND_OF_DAY_TYPE = new StandardDateTimeFieldType(
        "secondOfDay", DurationFieldType.seconds(), DurationFieldType.days());
    
The secondOfMinute field type.
    private static final DateTimeFieldType SECOND_OF_MINUTE_TYPE = new StandardDateTimeFieldType(
        "secondOfMinute", DurationFieldType.seconds(), DurationFieldType.minutes());
    
The millisOfDay field type.
    private static final DateTimeFieldType MILLIS_OF_DAY_TYPE = new StandardDateTimeFieldType(
        "millisOfDay", DurationFieldType.millis(), DurationFieldType.days());
    
The millisOfSecond field type.
    private static final DateTimeFieldType MILLIS_OF_SECOND_TYPE = new StandardDateTimeFieldType(
        "millisOfSecond", DurationFieldType.millis(), DurationFieldType.seconds());

    
The name of the field.
    private final String iName;
    //-----------------------------------------------------------------------
    
Constructor.

Parameters:
name the name to use
    protected DateTimeFieldType(String name) {
        super();
         = name;
    }
    //-----------------------------------------------------------------------
    
Get the millis of second field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType millisOfSecond() {
        return ;
    }

    
Get the millis of day field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType millisOfDay() {
        return ;
    }

    
Get the second of minute field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType secondOfMinute() {
        return ;
    }

    
Get the second of day field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType secondOfDay() {
        return ;
    }

    
Get the minute of hour field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType minuteOfHour() {
        return ;
    }

    
Get the minute of day field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType minuteOfDay() {
        return ;
    }

    
Get the hour of day (0-23) field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType hourOfDay() {
        return ;
    }

    
Get the hour of day (offset to 1-24) field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType clockhourOfDay() {
        return ;
    }

    
Get the hour of am/pm (0-11) field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType hourOfHalfday() {
        return ;
    }

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

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType clockhourOfHalfday() {
        return ;
    }

    
Get the AM(0) PM(1) field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType halfdayOfDay() {
        return ;
    }
    //-----------------------------------------------------------------------
    
Get the day of week field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType dayOfWeek() {
        return ;
    }

    
Get the day of month field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType dayOfMonth() {
        return ;
    }

    
Get the day of year field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType dayOfYear() {
        return ;
    }

    
Get the week of a week based year field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType weekOfWeekyear() {
        return ;
    }

    
Get the year of a week based year field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType weekyear() {
        return ;
    }

    
Get the year of a week based year within a century field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType weekyearOfCentury() {
        return ;
    }

    
Get the month of year field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType monthOfYear() {
        return ;
    }

    
Get the year field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType year() {
        return ;
    }

    
Get the year of era field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType yearOfEra() {
        return ;
    }

    
Get the year of century field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType yearOfCentury() {
        return ;
    }

    
Get the century of era field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType centuryOfEra() {
        return ;
    }

    
Get the era field type.

Returns:
the DateTimeFieldType constant
    public static DateTimeFieldType era() {
        return ;
    }
    //-----------------------------------------------------------------------
    
Get the name of the field.

By convention, names follow a pattern of "dddOfRrr", where "ddd" represents the (singular) duration unit field name and "Rrr" represents the (singular) duration range field name. If the range field is not applicable, then the name of the field is simply the (singular) duration field name.

Returns:
field name
    public String getName() {
        return ;
    }

    
Get the duration unit of the field.

Returns:
duration unit of the field, never null
    public abstract DurationFieldType getDurationType();

    
Get the duration range of the field.

Returns:
duration range of the field, null if unbounded
    public abstract DurationFieldType getRangeDurationType();

    
Gets a suitable field for this type from the given Chronology.

Parameters:
chronology the chronology to use, null means ISOChronology in default zone
Returns:
a suitable field
    public abstract DateTimeField getField(Chronology chronology);

    
Checks whether this field supported in the given Chronology.

Parameters:
chronology the chronology to use, null means ISOChronology in default zone
Returns:
true if supported
    public boolean isSupported(Chronology chronology) {
        return getField(chronology).isSupported();
    }

    
Get a suitable debug string.

Returns:
debug string
    public String toString() {
        return getName();
    }
    private static class StandardDateTimeFieldType extends DateTimeFieldType {
        
Serialization version
        private static final long serialVersionUID = -9937958251642L;

        
The ordinal of the standard field type, for switch statements
        private final byte iOrdinal;

        
The unit duration of the field.
        private final transient DurationFieldType iUnitType;
        
The range duration of the field.
        private final transient DurationFieldType iRangeType;

        
Constructor.

Parameters:
name the name to use
ordinal the byte value for the oridinal index
unitType the unit duration type
rangeType the range duration type
        StandardDateTimeFieldType(String namebyte ordinal,
                                  DurationFieldType unitTypeDurationFieldType rangeType) {
            super(name);
             = ordinal;
             = unitType;
             = rangeType;
        }

        

Inheritdoc:
        public DurationFieldType getDurationType() {
            return ;
        }

        

Inheritdoc:
        public DurationFieldType getRangeDurationType() {
            return ;
        }

        

Inheritdoc:
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof StandardDateTimeFieldType) {
                return  == ((StandardDateTimeFieldTypeobj).;
            }
            return false;
        }

        

Inheritdoc:
        @Override
        public int hashCode() {
            return ;
        }

        

Inheritdoc:
        public DateTimeField getField(Chronology chronology) {
            chronology = DateTimeUtils.getChronology(chronology);
            switch () {
                case :
                    return chronology.era();
                case :
                    return chronology.yearOfEra();
                case :
                    return chronology.centuryOfEra();
                case :
                    return chronology.yearOfCentury();
                case :
                    return chronology.year();
                case :
                    return chronology.dayOfYear();
                case :
                    return chronology.monthOfYear();
                case :
                    return chronology.dayOfMonth();
                case :
                    return chronology.weekyearOfCentury();
                case :
                    return chronology.weekyear();
                case :
                    return chronology.weekOfWeekyear();
                case :
                    return chronology.dayOfWeek();
                case :
                    return chronology.halfdayOfDay();
                case :
                    return chronology.hourOfHalfday();
                case :
                    return chronology.clockhourOfHalfday();
                case :
                    return chronology.clockhourOfDay();
                case :
                    return chronology.hourOfDay();
                case :
                    return chronology.minuteOfDay();
                case :
                    return chronology.minuteOfHour();
                case :
                    return chronology.secondOfDay();
                case :
                    return chronology.secondOfMinute();
                case :
                    return chronology.millisOfDay();
                case :
                    return chronology.millisOfSecond();
                default:
                    // Shouldn't happen.
                    throw new InternalError();
            }
        }

        
Ensure a singleton is returned.

Returns:
the singleton type
        private Object readResolve() {
            switch () {
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                case :
                    return ;
                default:
                    // Shouldn't happen.
                    return this;
            }
        }
    }
New to GrepCode? Check out our FAQ X