public class DateToken extends AbstractConvertibleToken implements PartiallyOrderedToken
This class tries to support both sub-millisecond precision
and string parsing of dates. This means that there are two
fields, a value, which is a Java long, and a calendar, which is
a java.util.Calendar. The Calendar class only support milliseconds,
it does not support microseconds and nanoseconds. When
operations are performed, it is essential that
setTimeInMilliseconds(long)
be called so that
both fields are updated.
Note: Java 8 provides a much improved implementation of dates and times. This implementation should be upgraded eventually.
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
_SIMPLE_DATE_FORMAT
The format in which dates are reported.
|
static DateToken |
NIL
A token that represents a missing value.
|
static int |
PRECISION_MICROSECOND
The flag indicating that the the precision is microseconds.
|
static int |
PRECISION_MILLISECOND
The flag indicating that the the precision is milliseconds.
|
static int |
PRECISION_NANOSECOND
The flag indicating that the the precision is nanoseconds.
|
static int |
PRECISION_SECOND
The flag indicating that the the precision is seconds.
|
Constructor and Description |
---|
DateToken()
Construct a date token.
|
DateToken(long value)
Construct a DateToken that represents the time since January 1, 1970.
|
DateToken(long value,
int precision)
Construct a DateToken that represents the time since January 1, 1970.
|
DateToken(long value,
int precision,
java.lang.String timeZone)
Construct a DateToken that represents the time since January 1, 1970.
|
DateToken(long value,
int precision,
java.util.TimeZone timeZone)
Construct a DateToken that represents the time since January 1, 1970.
|
DateToken(java.lang.String value)
Construct a DateToken that represents the time specified as a
string.
|
Modifier and Type | Method and Description |
---|---|
protected Token |
_add(Token rightArgument)
Subtract is not supported for Dates.
|
protected Token |
_divide(Token rightArgument)
Subtract is not supported for Dates.
|
protected BooleanToken |
_isCloseTo(Token token,
double epsilon)
The isCloseTo() method brings both tokens to the same precision.
|
protected BooleanToken |
_isEqualTo(Token rightArgument)
Return true of the the value of this token is equal
to the value of the argument according to java.util.Date.
|
protected BooleanToken |
_isLessThan(DateToken rightArgument)
Test for ordering of the values of this Token and the argument
Token.
|
protected Token |
_modulo(Token rightArgument)
Modulo is not supported for Dates.
|
protected Token |
_multiply(Token rightArgument)
Multiply is not supported for Dates.
|
protected Token |
_subtract(Token rightArgument)
Subtract is not supported for Dates.
|
void |
addMicroseconds(int microseconds)
Add microseconds to time.
|
void |
addNanoseconds(int nanoseconds)
Add nanoseconds to time.
|
static DateToken |
convert(Token token)
Convert the specified token into an instance of DateToken.
|
static DateToken |
date(java.lang.String value)
Create a DateToken with a value.
|
java.util.Calendar |
getCalendarInstance()
Get the calendar instance representing this date.
|
int |
getDay()
Get the date of the month part of this date.
|
int |
getDayOfWeek()
Get the day of week.
|
int |
getHour()
Get the hour part of this date.
|
int |
getMicrosecond()
Get the microsecond part of this date.
|
int |
getMillisecond()
Get the millisecond part of this date.
|
int |
getMinute()
Get the minute part of this date.
|
int |
getMonth()
Get the month part of this date.
|
int |
getNanosecond()
Get the nanosecond part of this date.
|
int |
getPrecision()
Get the precision of this date.
|
int |
getSecond()
Get the second part of this date.
|
long |
getTimeInMilliseconds()
Get time in milliseconds since January 1, 1970.
|
java.util.TimeZone |
getTimeZone()
Get the time zone of this date.
|
java.lang.String |
getTimezoneID()
Get the time zone id of this date.
|
Type |
getType()
Return the type of this token.
|
long |
getValue()
Get the time since January 1, 1970 in the given precision.
|
int |
getYear()
Get the year of this date.
|
BooleanToken |
isGreaterThan(PartiallyOrderedToken rightArgument)
Check whether the value of this token is strictly greater than
that of the argument token.
|
BooleanToken |
isLessThan(DateToken rightArgument)
Check whether the value of this token is strictly less than that of the
argument token.
|
BooleanToken |
isLessThan(PartiallyOrderedToken rightArgument)
Check whether the value of this token is strictly less than that of the
argument token.
|
boolean |
isNil()
Return true if the token is nil, (aka null or missing).
|
void |
setTimeInMilliseconds(long newValue)
Set the time in milliseconds since January 1, 1970.
|
java.lang.String |
stringValue()
Return the value of the token as a String.
|
java.lang.String |
toString()
Return a String representation of the DateToken.
|
add, addReverse, divide, divideReverse, isCloseTo, isEqualTo, modulo, moduloReverse, multiply, multiplyReverse, subtract, subtractReverse
isCloseTo, notSupportedConversionMessage, notSupportedIncomparableConversionMessage, notSupportedIncomparableMessage, notSupportedMessage, notSupportedNullNilStringMessage, one, pow, zero, zeroReturnType
public static final DateToken NIL
public static final int PRECISION_SECOND
public static final int PRECISION_MILLISECOND
public static final int PRECISION_MICROSECOND
public static final int PRECISION_NANOSECOND
protected static final java.lang.String _SIMPLE_DATE_FORMAT
public DateToken()
public DateToken(long value)
value
- The time since January 1, 1970 in the default precision
of milliseconds.public DateToken(long value, int precision)
value
- The time since January 1, 1970 in the given precision.precision
- The precision.public DateToken(long value, int precision, java.lang.String timeZone)
value
- The time since January 1, 1970 in the given precision.precision
- The precision.timeZone
- The string representation of the time zone ID.public DateToken(long value, int precision, java.util.TimeZone timeZone)
value
- The time since January 1, 1970 in the given precision.precision
- The precision.timeZone
- The time zone.public DateToken(java.lang.String value) throws IllegalActionException
_SIMPLE_DATE_FORMAT
is used.value
- The date specified in a format acceptable
to java.text.DateFormat.IllegalActionException
- If the date is not
parseable by java.text.DateFormat.public void addNanoseconds(int nanoseconds)
nanoseconds
- The nanoseconds to add.public void addMicroseconds(int microseconds)
microseconds
- The microseconds to add.public static DateToken convert(Token token) throws IllegalActionException
NIL
. Otherwise, if the argument is below DateToken in the
type hierarchy, it is converted to an instance of DateToken or
one of the subclasses of DateToken and returned. If none of
the above condition is met, an exception is thrown.token
- The token to be converted to a DateToken.IllegalActionException
- If the conversion
cannot be carried out.public java.util.Calendar getCalendarInstance()
public static DateToken date(java.lang.String value) throws IllegalActionException
value
- The date specified in a format acceptable
to java.text.DateFormat.IllegalActionException
- If thrown while creating
the DateToken.public int getDay()
public int getDayOfWeek()
public int getHour()
public int getMinute()
public int getMicrosecond()
public int getMillisecond()
public int getMonth()
public int getNanosecond()
public int getPrecision()
public int getSecond()
public java.util.TimeZone getTimeZone()
public java.lang.String getTimezoneID()
public long getValue()
public long getTimeInMilliseconds()
setTimeInMilliseconds(long)
public Type getType()
getType
in class Token
BaseType.DATE
,
the least upper bound of all the date types.public int getYear()
public final BooleanToken isGreaterThan(PartiallyOrderedToken rightArgument) throws IllegalActionException
rightArgument
- The token to compare against.IllegalActionException
- If the argument token and
this token are of incomparable types, or have different units,
or the operation does not make sense for the given types.public BooleanToken isLessThan(PartiallyOrderedToken rightArgument) throws IllegalActionException
isLessThan
in interface PartiallyOrderedToken
rightArgument
- The token on greater than side of the inequality.IllegalActionException
- If the tokens are incomparable.public BooleanToken isLessThan(DateToken rightArgument) throws IllegalActionException
rightArgument
- The token to compare against.IllegalActionException
- If the argument token and
this token are of incomparable types, or have different units,
or the operation does not make sense for the given types.public boolean isNil()
public void setTimeInMilliseconds(long newValue)
newValue
- The time as a long value.getTimeInMilliseconds()
public java.lang.String stringValue()
toString()
, except
toString() wraps the string value in double quotes.public java.lang.String toString()
Unfortunately, the Java Date class has a fatal flaw in that Date.toString() does not return the value of the number of ms., so we use a format that includes the number of ms.
protected Token _add(Token rightArgument) throws IllegalActionException
_add
in class AbstractConvertibleToken
rightArgument
- The token to subtract from this token.IllegalActionException
- Always thrown because
multiplying a Date does not make sense.protected Token _divide(Token rightArgument) throws IllegalActionException
_divide
in class AbstractConvertibleToken
rightArgument
- The token to subtract from this token.IllegalActionException
- Always thrown because
dividing a Date does not make sense.protected BooleanToken _isCloseTo(Token token, double epsilon) throws IllegalActionException
_isCloseTo
in class AbstractConvertibleToken
token
- The token to compare to this tokenepsilon
- the epsilonIllegalActionException
- Always thrown because
isCloseTo() on a Date does not make sense.protected BooleanToken _isEqualTo(Token rightArgument) throws IllegalActionException
_isEqualTo
in class AbstractConvertibleToken
rightArgument
- The token with which to test equality.IllegalActionException
- Not thrown in this baseclassprotected BooleanToken _isLessThan(DateToken rightArgument) throws IllegalActionException
rightArgument
- The token to compare to this token.IllegalActionException
- If this method is not
supported by the derived class.protected Token _modulo(Token rightArgument) throws IllegalActionException
_modulo
in class AbstractConvertibleToken
rightArgument
- The token to divide into this token.IllegalActionException
- Always thrown because
modulo of a Date does not make sense.protected Token _multiply(Token rightArgument) throws IllegalActionException
_multiply
in class AbstractConvertibleToken
rightArgument
- The token to multiply this token by.IllegalActionException
- Always thrown because
multiplying a Date does not make sense.protected Token _subtract(Token rightArgument) throws IllegalActionException
_subtract
in class AbstractConvertibleToken
rightArgument
- The token to subtract from this token.IllegalActionException
- Always thrown because
subtracting a Date does not make sense.