Class AbstractDateTime

  • All Implemented Interfaces:
    java.lang.Comparable<ReadableInstant>, ReadableDateTime, ReadableInstant
    Direct Known Subclasses:
    BaseDateTime

    public abstract class AbstractDateTime
    extends AbstractInstant
    implements ReadableDateTime
    AbstractDateTime provides the common behaviour for datetime classes.

    This class should generally not be used directly by API users. The 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.

    Since:
    1.0
    • Method Detail

      • get

        public int get​(DateTimeFieldType type)
        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.

        Specified by:
        get in interface ReadableInstant
        Overrides:
        get in class AbstractInstant
        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
      • getEra

        public int getEra()
        Get the era field value.
        Specified by:
        getEra in interface ReadableDateTime
        Returns:
        the era
      • getCenturyOfEra

        public int getCenturyOfEra()
        Get the year of era field value.
        Specified by:
        getCenturyOfEra in interface ReadableDateTime
        Returns:
        the year of era
      • getYearOfEra

        public int getYearOfEra()
        Get the year of era field value.
        Specified by:
        getYearOfEra in interface ReadableDateTime
        Returns:
        the year of era
      • getYearOfCentury

        public int getYearOfCentury()
        Get the year of century field value.
        Specified by:
        getYearOfCentury in interface ReadableDateTime
        Returns:
        the year of century
      • getYear

        public int getYear()
        Get the year field value.
        Specified by:
        getYear in interface ReadableDateTime
        Returns:
        the year
      • getWeekyear

        public int getWeekyear()
        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.

        Specified by:
        getWeekyear in interface ReadableDateTime
        Returns:
        the year of a week based year
      • getMonthOfYear

        public int getMonthOfYear()
        Get the month of year field value.
        Specified by:
        getMonthOfYear in interface ReadableDateTime
        Returns:
        the month of year
      • getWeekOfWeekyear

        public int getWeekOfWeekyear()
        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.

        Specified by:
        getWeekOfWeekyear in interface ReadableDateTime
        Returns:
        the week of a week based year
      • getDayOfYear

        public int getDayOfYear()
        Get the day of year field value.
        Specified by:
        getDayOfYear in interface ReadableDateTime
        Returns:
        the day of year
      • getDayOfMonth

        public int getDayOfMonth()
        Get the day of month field value.

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

        Specified by:
        getDayOfMonth in interface ReadableDateTime
        Returns:
        the day of month
      • getHourOfDay

        public int getHourOfDay()
        Get the hour of day field value.
        Specified by:
        getHourOfDay in interface ReadableDateTime
        Returns:
        the hour of day
      • getMinuteOfDay

        public int getMinuteOfDay()
        Get the minute of day field value.
        Specified by:
        getMinuteOfDay in interface ReadableDateTime
        Returns:
        the minute of day
      • getMinuteOfHour

        public int getMinuteOfHour()
        Get the minute of hour field value.
        Specified by:
        getMinuteOfHour in interface ReadableDateTime
        Returns:
        the minute of hour
      • getSecondOfDay

        public int getSecondOfDay()
        Get the second of day field value.
        Specified by:
        getSecondOfDay in interface ReadableDateTime
        Returns:
        the second of day
      • getSecondOfMinute

        public int getSecondOfMinute()
        Get the second of minute field value.
        Specified by:
        getSecondOfMinute in interface ReadableDateTime
        Returns:
        the second of minute
      • getMillisOfDay

        public int getMillisOfDay()
        Get the millis of day field value.
        Specified by:
        getMillisOfDay in interface ReadableDateTime
        Returns:
        the millis of day
      • getMillisOfSecond

        public int getMillisOfSecond()
        Get the millis of second field value.
        Specified by:
        getMillisOfSecond in interface ReadableDateTime
        Returns:
        the millis of second
      • toCalendar

        public java.util.Calendar toCalendar​(java.util.Locale locale)
        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 millisecond 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
      • toGregorianCalendar

        public java.util.GregorianCalendar toGregorianCalendar()
        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 millisecond 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
      • toString

        public java.lang.String toString()
        Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ).

        Note that this method does not output the chronology or time-zone. This can be confusing, as the equals and hashCode methods use both chronology and time-zone. If two objects are not equal but have the same toString then either the chronology or time-zone differs.

        Specified by:
        toString in interface ReadableInstant
        Overrides:
        toString in class AbstractInstant
        Returns:
        ISO8601 time formatted string, not null
      • toString

        public java.lang.String toString​(java.lang.String pattern)
        Output the instant using the specified format pattern.
        Specified by:
        toString in interface ReadableDateTime
        Parameters:
        pattern - the pattern specification, null means use toString
        Returns:
        the formatted string, not null
        See Also:
        DateTimeFormat
      • toString

        public java.lang.String toString​(java.lang.String pattern,
                                         java.util.Locale locale)
                                  throws java.lang.IllegalArgumentException
        Output the instant using the specified format pattern.
        Specified by:
        toString in interface ReadableDateTime
        Parameters:
        pattern - the pattern specification, null means use toString
        locale - Locale to use, null means default
        Returns:
        the formatted string, not null
        Throws:
        java.lang.IllegalArgumentException - if pattern is invalid
        See Also:
        DateTimeFormat