Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2011 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.base;
 
 
AbstractDateTime provides the common behaviour for datetime classes.

This class should generally not be used directly by API users. The org.joda.time.ReadableDateTime interface should be used when different kinds of date/time objects are to be referenced.

Whenever you want to implement ReadableDateTime you should extend this class.

AbstractDateTime subclasses may be mutable and not thread-safe.

Author(s):
Brian S O'Neill
Stephen Colebourne
Since:
1.0
 
 public abstract class AbstractDateTime
         extends AbstractInstant
         implements ReadableDateTime {

    
Constructor.
 
     protected AbstractDateTime() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Get the value of one of the fields of a datetime.

This method uses the chronology of the datetime to obtain the value. It is essentially a generic way of calling one of the get methods.

Parameters:
type a field type, usually obtained from DateTimeFieldType
Returns:
the value of that field
Throws:
java.lang.IllegalArgumentException if the field type is null
 
     public int get(DateTimeFieldType type) {
         if (type == null) {
             throw new IllegalArgumentException("The DateTimeFieldType must not be null");
         }
         return type.getField(getChronology()).get(getMillis());
     }
 
     //-----------------------------------------------------------------------
     
Get the era field value.

Returns:
the era
 
     public int getEra() {
         return getChronology().era().get(getMillis());
     }

    
Get the year of era field value.

Returns:
the year of era
 
     public int getCenturyOfEra() {
         return getChronology().centuryOfEra().get(getMillis());
     }

    
Get the year of era field value.

Returns:
the year of era
 
     public int getYearOfEra() {
         return getChronology().yearOfEra().get(getMillis());
     }

    
Get the year of century field value.

Returns:
the year of century
    public int getYearOfCentury() {
        return getChronology().yearOfCentury().get(getMillis());
    }

    
Get the year field value.

Returns:
the year
    public int getYear() {
        return getChronology().year().get(getMillis());
    }

    
Get the weekyear field value.

The weekyear is the year that matches with the weekOfWeekyear field. In the standard ISO8601 week algorithm, the first week of the year is that in which at least 4 days are in the year. As a result of this definition, day 1 of the first week may be in the previous year. The weekyear allows you to query the effective year for that day.

Returns:
the year of a week based year
    public int getWeekyear() {
        return getChronology().weekyear().get(getMillis());
    }

    
Get the month of year field value.

Returns:
the month of year
    public int getMonthOfYear() {
        return getChronology().monthOfYear().get(getMillis());
    }

    
Get the week of weekyear field value.

This field is associated with the "weekyear" via getWeekyear(). In the standard ISO8601 week algorithm, the first week of the year is that in which at least 4 days are in the year. As a result of this definition, day 1 of the first week may be in the previous year.

Returns:
the week of a week based year
    public int getWeekOfWeekyear() {
        return getChronology().weekOfWeekyear().get(getMillis());
    }

    
Get the day of year field value.

Returns:
the day of year
    public int getDayOfYear() {
        return getChronology().dayOfYear().get(getMillis());
    }

    
Get the day of month field value.

The values for the day of month are defined in org.joda.time.DateTimeConstants.

Returns:
the day of month
    public int getDayOfMonth() {
        return getChronology().dayOfMonth().get(getMillis());
    }

    
Get the day of week field value.

The values for the day of week are defined in org.joda.time.DateTimeConstants.

Returns:
the day of week
    public int getDayOfWeek() {
        return getChronology().dayOfWeek().get(getMillis());
    }
    //-----------------------------------------------------------------------
    
Get the hour of day field value.

Returns:
the hour of day
    public int getHourOfDay() {
        return getChronology().hourOfDay().get(getMillis());
    }

    
Get the minute of day field value.

Returns:
the minute of day
    public int getMinuteOfDay() {
        return getChronology().minuteOfDay().get(getMillis());
    }

    
Get the minute of hour field value.

Returns:
the minute of hour
    public int getMinuteOfHour() {
        return getChronology().minuteOfHour().get(getMillis());
    }

    
Get the second of day field value.

Returns:
the second of day
    public int getSecondOfDay() {
        return getChronology().secondOfDay().get(getMillis());
    }

    
Get the second of minute field value.

Returns:
the second of minute
    public int getSecondOfMinute() {
        return getChronology().secondOfMinute().get(getMillis());
    }

    
Get the millis of day field value.

Returns:
the millis of day
    public int getMillisOfDay() {
        return getChronology().millisOfDay().get(getMillis());
    }

    
Get the millis of second field value.

Returns:
the millis of second
    public int getMillisOfSecond() {
        return getChronology().millisOfSecond().get(getMillis());
    }
    //-----------------------------------------------------------------------
    
Get the date time as a java.util.Calendar, assigning exactly the same millisecond instant. The locale is passed in, enabling Calendar to select the correct localized subclass.

The JDK and Joda-Time both have time zone implementations and these differ in accuracy. Joda-Time's implementation is generally more up to date and thus more accurate - for example JDK1.3 has no historical data. The effect of this is that the field values of the Calendar may differ from those of this object, even though the milliseond value is the same. Most of the time this just means that the JDK field values are wrong, as our time zone information is more up to date.

Parameters:
locale the locale to get the Calendar for, or default if null
Returns:
a localized Calendar initialised with this datetime
    public Calendar toCalendar(Locale locale) {
        if (locale == null) {
            locale = Locale.getDefault();
        }
        DateTimeZone zone = getZone();
        Calendar cal = Calendar.getInstance(zone.toTimeZone(), locale);
        cal.setTime(toDate());
        return cal;
    }

    
Get the date time as a java.util.GregorianCalendar, assigning exactly the same millisecond instant.

The JDK and Joda-Time both have time zone implementations and these differ in accuracy. Joda-Time's implementation is generally more up to date and thus more accurate - for example JDK1.3 has no historical data. The effect of this is that the field values of the Calendar may differ from those of this object, even though the milliseond value is the same. Most of the time this just means that the JDK field values are wrong, as our time zone information is more up to date.

Returns:
a GregorianCalendar initialised with this datetime
        DateTimeZone zone = getZone();
        GregorianCalendar cal = new GregorianCalendar(zone.toTimeZone());
        cal.setTime(toDate());
        return cal;
    }
    //-----------------------------------------------------------------------
    
Output the instant using the specified format pattern.

Parameters:
pattern the pattern specification, null means use toString
See also:
org.joda.time.format.DateTimeFormat
    public String toString(String pattern) {
        if (pattern == null) {
            return toString();
        }
        return DateTimeFormat.forPattern(pattern).print(this);
    }

    
Output the instant using the specified format pattern.

Parameters:
pattern the pattern specification, null means use toString
locale Locale to use, null means default
See also:
org.joda.time.format.DateTimeFormat
    public String toString(String patternLocale localethrows IllegalArgumentException {
        if (pattern == null) {
            return toString();
        }
        return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this);
    }
New to GrepCode? Check out our FAQ X