Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2006 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.goda.time.base;
 
 import java.util.Date;
 
AbstractInstant provides the common behaviour for instant 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 org.goda.time.ReadableInstant interface should be used when different kinds of date/time objects are to be referenced.

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

AbstractInstant itself is thread-safe and immutable, but subclasses may be mutable and not thread-safe.

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

    
Constructor.
 
     protected AbstractInstant() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Gets the time zone of the instant from the chronology.

Returns:
the DateTimeZone that the instant is using, never null
 
     public DateTimeZone getZone() {
         return getChronology().getZone();
     }

    
Get the value of one of the fields of a datetime using the chronology of the instant.

This method uses the chronology of the instant to obtain the value. For example:

 DateTime dt = new DateTime();
 int year = dt.get(DateTimeFieldType.year());
 

Parameters:
type a field type, usually obtained from DateTimeFieldType, not null
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());
     }

    
Checks if the field type specified is supported by this instant and chronology. This can be used to avoid exceptions in get(org.goda.time.DateTimeFieldType).

Parameters:
type a field type, usually obtained from DateTimeFieldType
Returns:
true if the field type is supported
    public boolean isSupported(DateTimeFieldType type) {
        if (type == null) {
            return false;
        }
        return type.getField(getChronology()).isSupported();
    }

    
Get the value of one of the fields of a datetime.

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

 Instant dt = new Instant();
 int gjYear = dt.get(Chronology.getCoptic().year());
 

Parameters:
field the DateTimeField to use, not null
Returns:
the value
Throws:
java.lang.IllegalArgumentException if the field is null
    public int get(DateTimeField field) {
        if (field == null) {
            throw new IllegalArgumentException("The DateTimeField must not be null");
        }
        return field.get(getMillis());
    }
    //-----------------------------------------------------------------------
    
Get this object as an Instant.

Returns:
an Instant using the same millis
    public Instant toInstant() {
        return new Instant(getMillis());
    }

    
Get this object as a DateTime in the same zone.

Returns:
a DateTime using the same millis
    public DateTime toDateTime() {
        return new DateTime(getMillis(), getZone());
    }

    
Get this object as a DateTime using ISOChronology in the same zone.

Returns:
a DateTime using the same millis with ISOChronology
    public DateTime toDateTimeISO() {
        return new DateTime(getMillis(), ISOChronology.getInstance(getZone()));
    }

    
Get this object as a DateTime using the same chronology but a different zone.

Parameters:
zone time zone to apply, or default if null
Returns:
a DateTime using the same millis
    public DateTime toDateTime(DateTimeZone zone) {
        Chronology chrono = DateTimeUtils.getChronology(getChronology());
        chrono = chrono.withZone(zone);
        return new DateTime(getMillis(), chrono);
    }

    
Get this object as a DateTime using the given chronology and its zone.

Parameters:
chronology chronology to apply, or ISOChronology if null
Returns:
a DateTime using the same millis
    public DateTime toDateTime(Chronology chronology) {
        return new DateTime(getMillis(), chronology);
    }
    // NOTE: Although the toMutableDateTime methods could check to see if this
    // is already a MutableDateTime and return this casted, it makes it too
    // easy to mistakenly modify ReadableDateTime input parameters. Always
    // returning a copy prevents this.

    
Get this object as a MutableDateTime in the same zone.

Returns:
a MutableDateTime using the same millis
        return new MutableDateTime(getMillis(), getZone());
    }

    
Get this object as a MutableDateTime using ISOChronology in the same zone.

Returns:
a MutableDateTime using the same millis with ISOChronology
        return new MutableDateTime(getMillis(), ISOChronology.getInstance(getZone()));
    }

    
Get this object as a MutableDateTime using the same chronology but a different zone.

Parameters:
zone time zone to apply, or default if null
Returns:
a MutableDateTime using the same millis
        Chronology chrono = DateTimeUtils.getChronology(getChronology());
        chrono = chrono.withZone(zone);
        return new MutableDateTime(getMillis(), chrono);
    }

    
Get this object as a MutableDateTime using the given chronology and its zone.

Parameters:
chronology chronology to apply, or ISOChronology if null
Returns:
a MutableDateTime using the same millis
    public MutableDateTime toMutableDateTime(Chronology chronology) {
        return new MutableDateTime(getMillis(), chronology);
    }
    //-----------------------------------------------------------------------
    
Get the date time as a java.util.Date.

The Date object created has exactly the same millisecond instant as this object.

Returns:
a Date initialised with this datetime
    public Date toDate() {
        return new Date(getMillis());
    }
    //-----------------------------------------------------------------------
    
Compares this object with the specified object for equality based on the millisecond instant, chronology and time zone.

Two objects which represent the same instant in time, but are in different time zones (based on time zone id), will be considered to be different. Only two objects with the same org.goda.time.DateTimeZone, org.goda.time.Chronology and instant are equal.

See isEqual(org.goda.time.ReadableInstant) for an equals method that ignores the Chronology and time zone.

All ReadableInstant instances are accepted.

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
    public boolean equals(Object readableInstant) {
        // must be to fulfil ReadableInstant contract
        if (this == readableInstant) {
            return true;
        }
        if (readableInstant instanceof ReadableInstant == false) {
            return false;
        }
        ReadableInstant otherInstant = (ReadableInstantreadableInstant;
        return
            getMillis() == otherInstant.getMillis() &&
            FieldUtils.equals(getChronology(), otherInstant.getChronology());
    }

    
Gets a hash code for the instant as defined in ReadableInstant.

Returns:
a suitable hash code
    public int hashCode() {
        // must be to fulfil ReadableInstant contract
        return
            ((int) (getMillis() ^ (getMillis() >>> 32))) +
            (getChronology().hashCode());
    }

    
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.

Parameters:
instant a readable instant to check against
Returns:
negative value if this is less, 0 if equal, or positive value if greater
Throws:
java.lang.NullPointerException if the object is null
java.lang.ClassCastException if the object type is not supported
    public int compareTo(Object instant) {
        if (this == instant) {
            return 0;
        }
        
        ReadableInstant otherInstant = (ReadableInstantinstant;
        
        long otherMillis = otherInstant.getMillis();
        long thisMillis = getMillis();
        
        // cannot do (thisMillis - otherMillis) as can overflow
        if (thisMillis == otherMillis) {
            return 0;
        }
        if (thisMillis < otherMillis) {
            return -1;
        } else {
            return 1;
        }
    }
    //-----------------------------------------------------------------------
    
Is this instant after the millisecond instant passed in comparing solely by millisecond.

Parameters:
instant a millisecond instant to check against
Returns:
true if this instant is after the instant passed in
    public boolean isAfter(long instant) {
        return (getMillis() > instant);
    }

    
Is this instant after the current instant comparing solely by millisecond.

Returns:
true if this instant is after the current instant
    public boolean isAfterNow() {
        return isAfter(DateTimeUtils.currentTimeMillis());
    }

    
Is this instant after the instant passed in comparing solely by millisecond.

Parameters:
instant an instant to check against, null means now
Returns:
true if the instant is after the instant passed in
    public boolean isAfter(ReadableInstant instant) {
        long instantMillis = DateTimeUtils.getInstantMillis(instant);
        return isAfter(instantMillis);
    }
    //-----------------------------------------------------------------------
    
Is this instant before the millisecond instant passed in comparing solely by millisecond.

Parameters:
instant a millisecond instant to check against
Returns:
true if this instant is before the instant passed in
    public boolean isBefore(long instant) {
        return (getMillis() < instant);
    }

    
Is this instant before the current instant comparing solely by millisecond.

Returns:
true if this instant is before the current instant
    public boolean isBeforeNow() {
        return isBefore(DateTimeUtils.currentTimeMillis());
    }

    
Is this instant before the instant passed in comparing solely by millisecond.

Parameters:
instant an instant to check against, null means now
Returns:
true if the instant is before the instant passed in
    public boolean isBefore(ReadableInstant instant) {
        long instantMillis = DateTimeUtils.getInstantMillis(instant);
        return isBefore(instantMillis);
    }
    //-----------------------------------------------------------------------
    
Is this instant equal to the millisecond instant passed in comparing solely by millisecond.

Parameters:
instant a millisecond instant to check against
Returns:
true if this instant is before the instant passed in
    public boolean isEqual(long instant) {
        return (getMillis() == instant);
    }

    
Is this instant equal to the current instant comparing solely by millisecond.

Returns:
true if this instant is before the current instant
    public boolean isEqualNow() {
        return isEqual(DateTimeUtils.currentTimeMillis());
    }

    
Is this instant equal to the instant passed in comparing solely by millisecond.

Parameters:
instant an instant to check against, null means now
Returns:
true if the instant is equal to the instant passed in
    public boolean isEqual(ReadableInstant instant) {
        long instantMillis = DateTimeUtils.getInstantMillis(instant);
        return isEqual(instantMillis);
    }
    //-----------------------------------------------------------------------
    
Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZ).

Returns:
ISO8601 time formatted string.
    public String toString() {
        return ISODateTimeFormat.dateTime().print(this);
    }
    //-----------------------------------------------------------------------
    
Uses the specified formatter to convert this partial to a String.

Parameters:
formatter the formatter to use, null means use toString().
Returns:
the formatted string
Since:
1.1
    public String toString(DateTimeFormatter formatter) {
        if (formatter == null) {
            return toString();
        }
        return formatter.print(this);
    }
New to GrepCode? Check out our FAQ X