public abstract class

ImpreciseDateTimeField

extends BaseDateTimeField
java.lang.Object
   ↳ org.joda.time.DateTimeField
     ↳ org.joda.time.field.BaseDateTimeField
       ↳ org.joda.time.field.ImpreciseDateTimeField

Class Overview

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.

Summary

Public Constructors
ImpreciseDateTimeField(DateTimeFieldType type, long unitMillis)
Constructor.
Public Methods
abstract long add(long instant, long value)
Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.
abstract long add(long instant, int value)
Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.
abstract int get(long instant)
Get the value of this field from the milliseconds.
int getDifference(long minuendInstant, long subtrahendInstant)
Computes the difference between two instants, as measured in the units of this field.
long getDifferenceAsLong(long minuendInstant, long subtrahendInstant)
Computes the difference between two instants, as measured in the units of this field.
final DurationField getDurationField()
Returns the duration per unit value of this field.
abstract DurationField getRangeDurationField()
Returns the range duration of this field.
abstract long roundFloor(long instant)
Round to the lowest whole unit of this field.
abstract long set(long instant, int value)
Sets a value in the milliseconds supplied.
Protected Methods
final long getDurationUnitMillis()
[Expand]
Inherited Methods
From class org.joda.time.field.BaseDateTimeField
From class org.joda.time.DateTimeField
From class java.lang.Object

Public Constructors

public ImpreciseDateTimeField (DateTimeFieldType type, long unitMillis)

Constructor.

Parameters
type the field type
unitMillis the average duration unit milliseconds

Public Methods

public abstract long add (long instant, long value)

Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.

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

public abstract long add (long instant, int value)

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

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

public abstract int get (long instant)

Get the value of this field from the milliseconds.

Parameters
instant the milliseconds from 1970-01-01T00:00:00Z to query
Returns
  • the value of the field, in the units of the field

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.

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

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.

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

public final DurationField getDurationField ()

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.

Returns
  • the duration of this field, or UnsupportedDurationField if field has no duration

public abstract DurationField getRangeDurationField ()

Returns the range duration of this field. For example, if this field represents "hour of day", then the range duration is a day.

Returns
  • the range duration of this field, or null if field has no range

public abstract long roundFloor (long instant)

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.

Parameters
instant the milliseconds from 1970-01-01T00:00:00Z to round
Returns
  • rounded milliseconds

public abstract long set (long instant, int value)

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.

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

Protected Methods

protected final long getDurationUnitMillis ()