Class ImpreciseDateTimeField


public abstract class ImpreciseDateTimeField extends BaseDateTimeField
Abstract datetime field class that defines its own DurationField, which delegates back into this ImpreciseDateTimeField.

This DateTimeField is useful for defining DateTimeFields that are composed of imprecise durations. If both duration fields are precise, then a PreciseDateTimeField should be used instead.

When defining imprecise DateTimeFields where a matching DurationField is already available, just extend BaseDateTimeField directly so as not to create redundant DurationField instances.

ImpreciseDateTimeField is thread-safe and immutable, and its subclasses must be as well.

Since:
1.0
Author:
Brian S O'Neill
See Also:
  • Constructor Details

    • ImpreciseDateTimeField

      public ImpreciseDateTimeField(DateTimeFieldType type, long unitMillis)
      Constructor.
      Parameters:
      type - the field type
      unitMillis - the average duration unit milliseconds
  • Method Details

    • get

      public abstract int get(long instant)
      Description copied from class: BaseDateTimeField
      Get the value of this field from the milliseconds.
      Specified by:
      get in class BaseDateTimeField
      Parameters:
      instant - the milliseconds from 1970-01-01T00:00:00Z to query
      Returns:
      the value of the field, in the units of the field
    • set

      public abstract long set(long instant, int value)
      Description copied from class: BaseDateTimeField
      Sets a value in the milliseconds supplied.

      The value of this field will be set. If the value is invalid, an exception if thrown.

      If setting this field would make other fields invalid, then those fields may be changed. For example if the current date is the 31st January, and the month is set to February, the day would be invalid. Instead, the day would be changed to the closest value - the 28th/29th February as appropriate.

      Specified by:
      set in class BaseDateTimeField
      Parameters:
      instant - the milliseconds from 1970-01-01T00:00:00Z to set in
      value - the value to set, in the units of the field
      Returns:
      the updated milliseconds
    • add

      public abstract long add(long instant, int value)
      Description copied from class: BaseDateTimeField
      Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.

      The value will be added to this field. If the value is too large to be added solely to this field, larger fields will increase as required. Smaller fields should be unaffected, except where the result would be an invalid value for a smaller field. In this case the smaller field is adjusted to be in range.

      For example, in the ISO chronology:
      2000-08-20 add six months is 2001-02-20
      2000-08-20 add twenty months is 2002-04-20
      2000-08-20 add minus nine months is 1999-11-20
      2001-01-31 add one month is 2001-02-28
      2001-01-31 add two months is 2001-03-31

      Overrides:
      add in class BaseDateTimeField
      Parameters:
      instant - the milliseconds from 1970-01-01T00:00:00Z to add to
      value - the value to add, in the units of the field
      Returns:
      the updated milliseconds
    • add

      public abstract long add(long instant, long value)
      Description copied from class: BaseDateTimeField
      Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.
      Overrides:
      add in class BaseDateTimeField
      Parameters:
      instant - the milliseconds from 1970-01-01T00:00:00Z to add to
      value - the long value to add, in the units of the field
      Returns:
      the updated milliseconds
      See Also:
    • getDifference

      public int getDifference(long minuendInstant, long subtrahendInstant)
      Computes the difference between two instants, as measured in the units of this field. Any fractional units are dropped from the result. Calling getDifference reverses the effect of calling add. In the following code:
       long instant = ...
       int v = ...
       int age = getDifference(add(instant, v), instant);
       
      The value 'age' is the same as the value 'v'.

      The default implementation call getDifferenceAsLong and converts the return value to an int.

      Overrides:
      getDifference in class BaseDateTimeField
      Parameters:
      minuendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract from
      subtrahendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
      Returns:
      the difference in the units of this field
    • getDifferenceAsLong

      public long getDifferenceAsLong(long minuendInstant, long subtrahendInstant)
      Computes the difference between two instants, as measured in the units of this field. Any fractional units are dropped from the result. Calling getDifference reverses the effect of calling add. In the following code:
       long instant = ...
       long v = ...
       long age = getDifferenceAsLong(add(instant, v), instant);
       
      The value 'age' is the same as the value 'v'.

      The default implementation performs a guess-and-check algorithm using getDurationField().getUnitMillis() and the add() method. Subclasses are encouraged to provide a more efficient implementation.

      Overrides:
      getDifferenceAsLong in class BaseDateTimeField
      Parameters:
      minuendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract from
      subtrahendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
      Returns:
      the difference in the units of this field
    • getDurationField

      public final DurationField getDurationField()
      Description copied from class: BaseDateTimeField
      Returns the duration per unit value of this field. For example, if this field represents "hour of day", then the unit duration is an hour.
      Specified by:
      getDurationField in class BaseDateTimeField
      Returns:
      the duration of this field, or UnsupportedDurationField if field has no duration
    • getRangeDurationField

      public abstract DurationField getRangeDurationField()
      Description copied from class: BaseDateTimeField
      Returns the range duration of this field. For example, if this field represents "hour of day", then the range duration is a day.
      Specified by:
      getRangeDurationField in class BaseDateTimeField
      Returns:
      the range duration of this field, or null if field has no range
    • roundFloor

      public abstract long roundFloor(long instant)
      Description copied from class: BaseDateTimeField
      Round to the lowest whole unit of this field. After rounding, the value of this field and all fields of a higher magnitude are retained. The fractional millis that cannot be expressed in whole increments of this field are set to minimum.

      For example, a datetime of 2002-11-02T23:34:56.789, rounded to the lowest whole hour is 2002-11-02T23:00:00.000.

      Specified by:
      roundFloor in class BaseDateTimeField
      Parameters:
      instant - the milliseconds from 1970-01-01T00:00:00Z to round
      Returns:
      rounded milliseconds
    • getDurationUnitMillis

      protected final long getDurationUnitMillis()