Joda Time API

org.joda.time
Class AbstractInstant

java.lang.Object
  |
  +--org.joda.time.AbstractInstant
All Implemented Interfaces:
java.lang.Comparable, ReadableInstant, java.io.Serializable
Direct Known Subclasses:
AbstractDateTime, Instant

public abstract class AbstractInstant
extends java.lang.Object
implements ReadableInstant, java.io.Serializable

AbstractInstant provides the common behaviour for immutable time classes.

This class has no concept of a chronology, all methods work on the millisecond instant.

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

Since:
1.0
Author:
Stephen Colebourne, Brian S O'Neill
See Also:
Serialized Form

Constructor Summary
AbstractInstant()
          Constructor.
 
Method Summary
 int compareTo(java.lang.Object readableInstant)
          Compares this object with the specified object for ascending millisecond instant order.
protected abstract  ReadableInstant create(long millis, Chronology chrono)
          Creates a new instance of this class.
 boolean equals(java.lang.Object readableInstant)
          Compares this object with the specified object for equality based on the millisecond instant and the Chronology.
 int get(DateTimeField field)
          Get the value of the specified field.
 DateTimeZone getDateTimeZone()
          Gets the time zone of the datetime from the chronology.
 int hashCode()
          Gets a hash code for the instant that is compatable with the equals method.
 boolean isAfter(ReadableInstant readableInstant)
          Is the millisecond value after the millisecond passed in.
 boolean isBefore(ReadableInstant readableInstant)
          Is the millisecond value before the millisecond passed in.
 boolean isEqual(ReadableInstant readableInstant)
          Is the millisecond value equal to the millisecond passed in.
 java.util.Calendar toCalendar(java.util.Locale locale)
          Get the date time as a java.util.Calendar.
 ReadableInstant toCopy(Chronology newChronology)
          Gets a copy of this instant with a different chronology.
 ReadableInstant toCopy(long newMillis)
          Gets a copy of this instant with different millis.
 java.util.Date toDate()
          Get the date time as a java.util.Date.
 java.util.GregorianCalendar toGregorianCalendar()
          Get the date time as a java.util.GregorianCalendar.
 Instant toInstant()
          Get this object as an Instant.
abstract  java.lang.String toString()
          Get the value as a String in a recognisable ISO8601 format.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.joda.time.ReadableInstant
getChronology, getMillis
 

Constructor Detail

AbstractInstant

public AbstractInstant()
Constructor.

Method Detail

getDateTimeZone

public final DateTimeZone getDateTimeZone()
Gets the time zone of the datetime from the chronology.

Specified by:
getDateTimeZone in interface ReadableInstant
Returns:
the DateTimeZone that the datetime is using

create

protected abstract ReadableInstant create(long millis,
                                          Chronology chrono)
Creates a new instance of this class.

The returned object will be a new instance of the implementation. Immutable subclasses may return this if appropriate.

Parameters:
millis - the new millis, from 1970-01-01T00:00:00Z
chrono - the new chronology
Returns:
a new instance of this class
Throws:
java.lang.IllegalArgumentException - if the chronology is null

get

public final int get(DateTimeField field)
Get the value of the specified field.

This could be used to get a field using a different Chronology. For example:

 Instant dt = new Instant();
 int gjYear = dt.get(ISOChronology.getInstance().year());
 

Specified by:
get in interface ReadableInstant
Parameters:
field - the DateTimeField subclass to use
Returns:
the value
Throws:
java.lang.IllegalArgumentException - if the field is null

toCopy

public final ReadableInstant toCopy(long newMillis)
Gets a copy of this instant with different millis.

The returned object will be a new instance of DateTime. Only the millis will change, the chronology and time zone are kept. If the millis is the same, this will be returned.

Specified by:
toCopy in interface ReadableInstant
Parameters:
newMillis - the new millis, from 1970-01-01T00:00:00Z
Returns:
a copy of this instant with different millis

toCopy

public final ReadableInstant toCopy(Chronology newChronology)
Gets a copy of this instant with a different chronology.

The returned object will be a new instance of DateTime. Only the chronology will change, the millis are kept. If the chronology is the same, this will be returned.

Specified by:
toCopy in interface ReadableInstant
Parameters:
newChronology - the new chronology
Returns:
a copy of this instant with a different chronology
Throws:
java.lang.IllegalArgumentException - if the chronology is null

toInstant

public final Instant toInstant()
Get this object as an Instant.

Specified by:
toInstant in interface ReadableInstant
Returns:
an Instant using the same millis

toDate

public final java.util.Date toDate()
Get the date time as a java.util.Date.

Returns:
a Date initialised with this datetime

toCalendar

public final java.util.Calendar toCalendar(java.util.Locale locale)
Get the date time as a java.util.Calendar. The locale is passed in, enabling Calendar to select the correct localized subclass.

Parameters:
locale - the locale to get the Calendar for
Returns:
a localized Calendar initialised with this datetime
Throws:
java.lang.IllegalArgumentException - if the locale is null

toGregorianCalendar

public final java.util.GregorianCalendar toGregorianCalendar()
Get the date time as a java.util.GregorianCalendar.

Returns:
a GregorianCalendar initialised with this datetime

equals

public final boolean equals(java.lang.Object readableInstant)
Compares this object with the specified object for equality based on the millisecond instant and the Chronology.

All ReadableInstant instances are accepted.

See isEqual(ReadableInstant) for an equals method that ignores the Chronology.

Specified by:
equals in interface ReadableInstant
Overrides:
equals in class java.lang.Object
Parameters:
readableInstant - a readable instant to check against
Returns:
true if millisecond and chronology are equal, false if not or the instant is null or of an incorrect type

hashCode

public final int hashCode()
Gets a hash code for the instant that is compatable with the equals method.

Specified by:
hashCode in interface ReadableInstant
Overrides:
hashCode in class java.lang.Object
Returns:
a suitable hash code

compareTo

public final int compareTo(java.lang.Object readableInstant)
Compares this object with the specified object for ascending millisecond instant order. This ordering is inconsistent with equals, as it ignores the Chronology.

All ReadableInstant instances are accepted.

Specified by:
compareTo in interface ReadableInstant
Parameters:
readableInstant - a readable instant to check against
Returns:
-1 if this is less, 0 if equal or +1 if greater
Throws:
java.lang.NullPointerException - if the object is null
java.lang.ClassCastException - if the object type is not supported

isAfter

public final boolean isAfter(ReadableInstant readableInstant)
Is the millisecond value after the millisecond passed in.

Specified by:
isAfter in interface ReadableInstant
Parameters:
readableInstant - an instant to check against
Returns:
true if the instant is after the instant passed in
Throws:
java.lang.IllegalArgumentException - if the object is null

isBefore

public final boolean isBefore(ReadableInstant readableInstant)
Is the millisecond value before the millisecond passed in.

Specified by:
isBefore in interface ReadableInstant
Parameters:
readableInstant - an instant to check against
Returns:
true if the instant is before the instant passed in
Throws:
java.lang.IllegalArgumentException - if the object is null

isEqual

public final boolean isEqual(ReadableInstant readableInstant)
Is the millisecond value equal to the millisecond passed in.

Specified by:
isEqual in interface ReadableInstant
Parameters:
readableInstant - an instant to check against
Returns:
true if the instant is equal to the instant passed in
Throws:
java.lang.IllegalArgumentException - if the object is null

toString

public abstract java.lang.String toString()
Get the value as a String in a recognisable ISO8601 format.

The string output is in ISO8601 format to enable the String constructor to correctly parse it.

Specified by:
toString in interface ReadableInstant
Overrides:
toString in class java.lang.Object
Returns:
the value as an ISO8601 string

Joda Time API

Copyright © 2001-2003 Stephen Colebourne. All Rights Reserved.