Joda Time API

org.joda.time
Class DateTimeField

java.lang.Object
  |
  +--org.joda.time.DateTimeField
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
DelegateDateTimeField, DividedDateTimeField, FractionalDateTimeField, LimitDateTimeField, NonZeroDateTimeField, OffsetDateTimeField, RemainderDateTimeField

public abstract class DateTimeField
extends java.lang.Object
implements java.io.Serializable

DateTimeField is an abstract class which allows the date and time manipulation code to be field based. Each field within a datetime can be accessed and manipulated using a dedicated subclass of this one.

This design is extensible, so if you wish to extract a different field from the milliseconds, you can do, simply by implementing your own subclass.

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

Constructor Summary
protected DateTimeField(java.lang.String name)
          Constructor.
 
Method Summary
abstract  long add(long millis, int value)
          Adds a value (which may be negative) to the millis value, overflowing into larger fields if necessary.
abstract  long add(long millis, long value)
          Adds a value (which may be negative) to the millis value, overflowing into larger fields if necessary.
protected  long addLong(long millis, long value)
          If value can be safely cast to an int, then add(long, int) is called, returning its result.
 long addWrapped(long millis, int value)
          Adds a value (which may be negative) to the millis value, wrapping within this field.
abstract  int get(long millis)
          Get the value of this field from the milliseconds.
 java.lang.String getAsShortText(long millis)
          Get the human-readable, short text value of this field from the milliseconds.
 java.lang.String getAsShortText(long millis, java.util.Locale locale)
          Get the human-readable, short text value of this field from the milliseconds.
 java.lang.String getAsText(long millis)
          Get the human-readable, text value of this field from the milliseconds.
 java.lang.String getAsText(long millis, java.util.Locale locale)
          Get the human-readable, text value of this field from the milliseconds.
 long getDifference(long minuendMillis, long subtrahendMillis)
          Computes the difference between two instants, as measured in the units of this field.
 int getLeapAmount(long millis)
          Gets the amount by which this field is 'leap' for the specified millis.
 int getMaximumShortTextLength(java.util.Locale locale)
          Get the maximum short text value for this field.
 int getMaximumTextLength(java.util.Locale locale)
          Get the maximum text value for this field.
abstract  int getMaximumValue()
          Get the maximum allowable value for this field.
 int getMaximumValue(long millis)
          Get the maximum value for this field evaluated at the specified time.
abstract  int getMinimumValue()
          Get the minimum allowable value for this field.
 int getMinimumValue(long millis)
          Get the minimum value for this field evaluated at the specified time.
 java.lang.String getName()
          Get the name of the field.
abstract  long getRangeMillis()
          Returns the range of this field, in milliseconds.
abstract  long getUnitMillis()
          Returns the amount of milliseconds per unit value of this field.
protected  int getWrappedValue(int value, int minValue, int maxValue)
          Utility method that ensures the given value lies within the field's legal value range.
protected  int getWrappedValue(int currentValue, int wrapValue, int minValue, int maxValue)
          Utility method used by addWrapped implementations to ensure the new value lies within the field's legal value range.
 boolean isLeap(long millis)
          Returns whether this field is 'leap' for the specified millis.
 long remainder(long millis)
          Retains only the fractional units of this field.
 long roundCeiling(long millis)
          Round to the highest whole unit of this field.
abstract  long roundFloor(long millis)
          Round to the lowest whole unit of this field.
 long roundHalfCeiling(long millis)
          Round to the nearest whole unit of this field.
 long roundHalfEven(long millis)
          Round to the nearest whole unit of this field.
 long roundHalfFloor(long millis)
          Round to the nearest whole unit of this field.
abstract  long set(long millis, int value)
          Sets a value in the milliseconds supplied.
 long set(long millis, java.lang.String text)
          Sets a value in the milliseconds supplied from a human-readable, text value.
 long set(long millis, java.lang.String text, java.util.Locale locale)
          Sets a value in the milliseconds supplied from a human-readable, text value.
 java.lang.String toString()
          Get a suitable debug string.
protected  void verifyValueBounds(int value, int lowerBound, int upperBound)
          Verify that input values are within specified bounds.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DateTimeField

protected DateTimeField(java.lang.String name)
Constructor.

Parameters:
name - a short descriptive name for the field, such as millisOfSecond
Method Detail

getName

public java.lang.String getName()
Get the name of the field.

Returns:
field name

get

public abstract int get(long millis)
Get the value of this field from the milliseconds.

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

getAsText

public java.lang.String getAsText(long millis,
                                  java.util.Locale locale)
Get the human-readable, text value of this field from the milliseconds. If the specified locale is null, the default locale is used.

The default implementation returns Integer.toString(get(millis)).

Note: subclasses that override this method should also override getMaximumTextLength.

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to query
locale - the locale to use for selecting a text symbol, null for default
Returns:
the text value of the field

getAsText

public final java.lang.String getAsText(long millis)
Get the human-readable, text value of this field from the milliseconds. This implementation returns getAsText(millis, null).

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to query
Returns:
the text value of the field

getAsShortText

public java.lang.String getAsShortText(long millis,
                                       java.util.Locale locale)
Get the human-readable, short text value of this field from the milliseconds. If the specified locale is null, the default locale is used.

The default implementation returns getAsText(millis, locale).

Note: subclasses that override this method should also override getMaximumShortTextLength.

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to query
locale - the locale to use for selecting a text symbol, null for default
Returns:
the short text value of the field

getAsShortText

public final java.lang.String getAsShortText(long millis)
Get the human-readable, short text value of this field from the milliseconds. This implementation returns getAsShortText(millis, null).

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to query
Returns:
the short text value of the field

add

public abstract long add(long millis,
                         int value)
Adds a value (which may be negative) to the millis 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:
millis - 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 millis,
                         long value)
Adds a value (which may be negative) to the millis value, overflowing into larger fields if necessary.

Parameters:
millis - 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
Throws:
java.lang.IllegalArgumentException - if value is too large
See Also:
add(long,int)

addWrapped

public long addWrapped(long millis,
                       int value)
Adds a value (which may be negative) to the millis value, wrapping within this field.

The value will be added to this field. If the value is too large to be added solely to this field then it wraps. Larger fields are always unaffected. 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 addWrapped six months is 2000-02-20
2000-08-20 addWrapped twenty months is 2000-04-20
2000-08-20 addWrapped minus nine months is 2000-11-20
2001-01-31 addWrapped one month is 2001-02-28
2001-01-31 addWrapped two months is 2001-03-31

Parameters:
millis - 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

getDifference

public long getDifference(long minuendMillis,
                          long subtrahendMillis)
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 millis = ...
 int v = ...
 long age = getDifference(add(millis, v), millis);
 
The value 'age' is the same as the value 'v'.

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

Parameters:
minuendMillis - the milliseconds from 1970-01-01T00:00:00Z to subtract from
subtrahendMillis - the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
Returns:
the difference in the units of this field

set

public abstract long set(long millis,
                         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. Other fields are always unaffected.

Parameters:
millis - 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
Throws:
java.lang.IllegalArgumentException - if the value is invalid

set

public long set(long millis,
                java.lang.String text,
                java.util.Locale locale)
Sets a value in the milliseconds supplied from a human-readable, text value. If the specified locale is null, the default locale is used.

The default implementation returns set(millis, Integer.parseInt(millis)).

Note: subclasses that override this method should also override getAsText.

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to set in
text - the text value to set
locale - the locale to use for selecting a text symbol, null for default
Returns:
the updated milliseconds
Throws:
java.lang.IllegalArgumentException - if the text value is invalid

set

public final long set(long millis,
                      java.lang.String text)
Sets a value in the milliseconds supplied from a human-readable, text value. This implementation returns set(millis, text, null).

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to set in
text - the text value to set
Returns:
the updated milliseconds
Throws:
java.lang.IllegalArgumentException - if the text value is invalid

isLeap

public boolean isLeap(long millis)
Returns whether this field is 'leap' for the specified millis.

For example, a leap year would return true, a non leap year would return false.

This implementation returns false.

Returns:
true if the field is 'leap'

getLeapAmount

public int getLeapAmount(long millis)
Gets the amount by which this field is 'leap' for the specified millis.

For example, a leap year would return one, a non leap year would return zero.

This implementation returns zero.


getUnitMillis

public abstract long getUnitMillis()
Returns the amount of milliseconds per unit value of this field. For example, if this field represents "hour of day", then the unit is the amount of milliseconds per one hour.

For fields with a variable unit size, this method returns a suitable average value.

Returns:
the unit size of this field, in milliseconds

getRangeMillis

public abstract long getRangeMillis()
Returns the range of this field, in milliseconds. For example, if this field represents "hour of day", then the range is the amount of milliseconds per one day.

For fields with a variable range, this method returns a suitable average value. If the range is too large to fit in a long, Long.MAX_VALUE is returned.

Returns:
the range of this field, in milliseconds

getMinimumValue

public abstract int getMinimumValue()
Get the minimum allowable value for this field.

Returns:
the minimum valid value for this field, in the units of the field

getMinimumValue

public int getMinimumValue(long millis)
Get the minimum value for this field evaluated at the specified time.

This implementation returns the same as getMinimumValue().

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to query
Returns:
the minimum value for this field, in the units of the field

getMaximumValue

public abstract int getMaximumValue()
Get the maximum allowable value for this field.

Returns:
the maximum valid value for this field, in the units of the field

getMaximumValue

public int getMaximumValue(long millis)
Get the maximum value for this field evaluated at the specified time.

This implementation returns the same as getMaximumValue().

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to query
Returns:
the maximum value for this field, in the units of the field

getMaximumTextLength

public int getMaximumTextLength(java.util.Locale locale)
Get the maximum text value for this field. The default implementation returns the equivalent of Integer.toString(getMaximumValue()).length().

Parameters:
locale - the locale to use for selecting a text symbol
Returns:
the maximum text length

getMaximumShortTextLength

public int getMaximumShortTextLength(java.util.Locale locale)
Get the maximum short text value for this field. The default implementation returns getMaximumTextLength().

Parameters:
locale - the locale to use for selecting a text symbol
Returns:
the maximum short text length

roundFloor

public abstract long roundFloor(long millis)
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:
millis - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundCeiling

public long roundCeiling(long millis)
Round to the highest whole unit of this field. The value of this field and all fields of a higher magnitude may be incremented in order to achieve this result. 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 highest whole hour is 2002-11-03T00:00:00.000.

The default implementation calls roundFloor, and if the millis is modified as a result, adds one field unit. Subclasses are encouraged to provide a more efficient implementation.

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

roundHalfFloor

public long roundHalfFloor(long millis)
Round to the nearest whole unit of this field. If the given millisecond value is closer to the floor or is exactly halfway, this function behaves like roundFloor. If the millisecond value is closer to the ceiling, this function behaves like roundCeiling.

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

roundHalfCeiling

public long roundHalfCeiling(long millis)
Round to the nearest whole unit of this field. If the given millisecond value is closer to the floor, this function behaves like roundFloor. If the millisecond value is closer to the ceiling or is exactly halfway, this function behaves like roundCeiling.

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

roundHalfEven

public long roundHalfEven(long millis)
Round to the nearest whole unit of this field. If the given millisecond value is closer to the floor, this function behaves like roundFloor. If the millisecond value is closer to the ceiling, this function behaves like roundCeiling.

If the millisecond value is exactly halfway between the floor and ceiling, the ceiling is chosen over the floor only if it makes this field's value even.

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

remainder

public long remainder(long millis)
Retains only the fractional units of this field. This field value and all fields of higher magnitude are reset. In other words, calling remainder retains the part of the instant that roundFloor dropped.

For example, a datetime of 2002-11-02T23:34:56.789, the remainder by hour is 1970-01-01T00:34:56.789.

The default implementation computes millis - roundFloor(millis). Subclasses are encouraged to provide a more efficient implementation.

Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to get the remainder
Returns:
remainder milliseconds

verifyValueBounds

protected void verifyValueBounds(int value,
                                 int lowerBound,
                                 int upperBound)
Verify that input values are within specified bounds.

Parameters:
value - the value to check
lowerBound - the lower bound allowed for value
upperBound - the upper bound allowed for value
Throws:
java.lang.IllegalArgumentException - if value is not in the specified bounds

getWrappedValue

protected final int getWrappedValue(int currentValue,
                                    int wrapValue,
                                    int minValue,
                                    int maxValue)
Utility method used by addWrapped implementations to ensure the new value lies within the field's legal value range.

Parameters:
currentValue - the current value of the data, which may lie outside the wrapped value range
wrapValue - the value to add to current value before wrapping. This may be negative.
minValue - the wrap range minimum value.
maxValue - the wrap range maximum value. This must be greater than minValue (checked by the method).
Returns:
the wrapped value
Throws:
java.lang.IllegalArgumentException - if minValue is greater than or equal to maxValue

getWrappedValue

protected final int getWrappedValue(int value,
                                    int minValue,
                                    int maxValue)
Utility method that ensures the given value lies within the field's legal value range.

Parameters:
value - the value to fit into the wrapped value range
minValue - the wrap range minimum value.
maxValue - the wrap range maximum value. This must be greater than minValue (checked by the method).
Returns:
the wrapped value
Throws:
java.lang.IllegalArgumentException - if minValue is greater than or equal to maxValue

addLong

protected long addLong(long millis,
                       long value)
If value can be safely cast to an int, then add(long, int) is called, returning its result. Otherwise, an IllegalArgumentException is thrown.

Parameters:
millis - 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
Throws:
java.lang.IllegalArgumentException - if value is too large

toString

public java.lang.String toString()
Get a suitable debug string.

Overrides:
toString in class java.lang.Object
Returns:
debug string

Joda Time API

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