Joda Time API

org.joda.time.chrono
Class LimitDateTimeField

java.lang.Object
  |
  +--org.joda.time.DateTimeField
        |
        +--org.joda.time.chrono.LimitDateTimeField
All Implemented Interfaces:
java.io.Serializable

public class LimitDateTimeField
extends DateTimeField

Generic limiting datetime field.

This DateTimeField allows specific millisecond boundaries to be applied to DateTimeFields.

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

Constructor Summary
LimitDateTimeField(java.lang.String name, DateTimeField field, long lowerBound, long upperBound)
          Constructor
 
Method Summary
 long add(long millis, int amount)
          Add the specified amount of fractional units to the specified time instant.
 long add(long millis, long amount)
          Add the specified amount of fractional units to the specified time instant.
 long addWrapped(long millis, int amount)
          Add to the fractional component of the specified time instant, wrapping around within that component if necessary.
 int get(long millis)
          Get the amount of fractional units from the specified time instant.
 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, 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.
 DateTimeField getField()
          Returns the DateTimeField being wrapped.
 long getLowerBound()
          Returns the milliseconds lower bound.
 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.
 int getMaximumValue()
          Get the maximum value for the field.
 int getMinimumValue()
          Get the minimum value for the field.
 long getRangeMillis()
          Returns the range of this field, in milliseconds.
 long getUnitMillis()
          Returns the amount of milliseconds per unit value of this field.
 long getUpperBound()
          Returns the milliseconds upper bound.
 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.
 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.
 long set(long millis, int value)
          Set the specified amount of fractional units to the specified time instant.
 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.
 
Methods inherited from class org.joda.time.DateTimeField
addLong, getAsShortText, getAsText, getLeapAmount, getMaximumValue, getMinimumValue, getName, getWrappedValue, getWrappedValue, isLeap, set, toString, verifyValueBounds
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LimitDateTimeField

public LimitDateTimeField(java.lang.String name,
                          DateTimeField field,
                          long lowerBound,
                          long upperBound)
Constructor

Parameters:
name - short, descriptive name, like "secondOfMinute".
lowerBound - milliseconds to form the lower boundary inclusive
upperBound - milliseconds to form the upper boundary inclusive
Throws:
java.lang.IllegalArgumentException - if field is null or boundary is invalid
Method Detail

get

public int get(long millis)
Get the amount of fractional units from the specified time instant.

Specified by:
get in class DateTimeField
Parameters:
millis - the time instant in millis to query.
Returns:
the amount of fractional units extracted from the input.

getAsText

public java.lang.String getAsText(long millis,
                                  java.util.Locale locale)
Description copied from class: DateTimeField
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.

Overrides:
getAsText in class DateTimeField
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

getAsShortText

public java.lang.String getAsShortText(long millis,
                                       java.util.Locale locale)
Description copied from class: DateTimeField
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.

Overrides:
getAsShortText in class DateTimeField
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

add

public long add(long millis,
                int amount)
Add the specified amount of fractional units to the specified time instant. The amount added may be negative.

Specified by:
add in class DateTimeField
Parameters:
millis - the time instant in millis to update.
amount - the amount of fractional units to add (can be negative).
Returns:
the updated time instant.

add

public long add(long millis,
                long amount)
Add the specified amount of fractional units to the specified time instant. The amount added may be negative.

Specified by:
add in class DateTimeField
Parameters:
millis - the time instant in millis to update.
amount - the amount of fractional units to add (can be negative).
Returns:
the updated time instant.
See Also:
DateTimeField.add(long,int)

addWrapped

public long addWrapped(long millis,
                       int amount)
Add to the fractional component of the specified time instant, wrapping around within that component if necessary.

Overrides:
addWrapped in class DateTimeField
Parameters:
millis - the time instant in millis to update.
amount - the amount of fractional units to add (can be negative).
Returns:
the updated time instant.

getDifference

public long getDifference(long minuendMillis,
                          long subtrahendMillis)
Description copied from class: DateTimeField
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.

Overrides:
getDifference in class DateTimeField
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 long set(long millis,
                int value)
Set the specified amount of fractional units to the specified time instant.

Specified by:
set in class DateTimeField
Parameters:
millis - the time instant in millis to update.
value - value of fractional units to set.
Returns:
the updated time instant.
Throws:
java.lang.IllegalArgumentException - if value is too large or too small.

set

public long set(long millis,
                java.lang.String text,
                java.util.Locale locale)
Description copied from class: DateTimeField
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.

Overrides:
set in class DateTimeField
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

getUnitMillis

public long getUnitMillis()
Description copied from class: DateTimeField
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.

Specified by:
getUnitMillis in class DateTimeField
Returns:
the unit size of this field, in milliseconds

getRangeMillis

public long getRangeMillis()
Description copied from class: DateTimeField
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.

Specified by:
getRangeMillis in class DateTimeField
Returns:
the range of this field, in milliseconds

getMinimumValue

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

Specified by:
getMinimumValue in class DateTimeField
Returns:
the minimum value

getMaximumValue

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

Specified by:
getMaximumValue in class DateTimeField
Returns:
the maximum value

getMaximumTextLength

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

Overrides:
getMaximumTextLength in class DateTimeField
Parameters:
locale - the locale to use for selecting a text symbol
Returns:
the maximum text length

getMaximumShortTextLength

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

Overrides:
getMaximumShortTextLength in class DateTimeField
Parameters:
locale - the locale to use for selecting a text symbol
Returns:
the maximum short text length

roundFloor

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

roundCeiling

public long roundCeiling(long millis)
Description copied from class: DateTimeField
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.

Overrides:
roundCeiling in class DateTimeField
Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundHalfFloor

public long roundHalfFloor(long millis)
Description copied from class: DateTimeField
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.

Overrides:
roundHalfFloor in class DateTimeField
Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundHalfCeiling

public long roundHalfCeiling(long millis)
Description copied from class: DateTimeField
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.

Overrides:
roundHalfCeiling in class DateTimeField
Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundHalfEven

public long roundHalfEven(long millis)
Description copied from class: DateTimeField
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.

Overrides:
roundHalfEven in class DateTimeField
Parameters:
millis - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

remainder

public long remainder(long millis)
Description copied from class: DateTimeField
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.

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

getField

public DateTimeField getField()
Returns the DateTimeField being wrapped.

Returns:
field

getLowerBound

public long getLowerBound()
Returns the milliseconds lower bound.

Returns:
lower bound

getUpperBound

public long getUpperBound()
Returns the milliseconds upper bound.

Returns:
upper bound

Joda Time API

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