org.exolab.castor.types

Class DateTimeBase

Implemented Interfaces:
Cloneable, java.io.Serializable
Known Direct Subclasses:
Date, DateTime, GDay, GMonth, GMonthDay, GYear, GYearMonth, Time

public abstract class DateTimeBase
extends java.lang.Object
implements java.io.Serializable, Cloneable

The base class for date/time XML Schema types.

The validation of the date/time fields is done in the set methods and follows the ISO8601 Date and Time Format.

Note: the Castor date/time types are mutable, unlike the date/time types of the JDK in Java2. This is needed by the Marshaling framework.

Version:
$Revision: 7289 $
Authors:
Arnaud Blandin
Edward Kuns
See Also:
DateTime, Date, Time, Serialized Form

Field Summary

static int
EQUALS
Public constant referring to a Date/Time comparison result of "equals".
static int
GREATER_THAN
Public constant referring to a Date/Time comparison result of "greater than".
static int
INDETERMINATE
Public constant referring to an indeterminate Date/Time comparison.
static int
LESS_THAN
Public constant referring to a Date/Time comparison result of "less than".
protected static int
MAX_TIME_ZONE_COMPARISON_OFFSET
When comparing a date/time with a time zone to one without, the recommendation says that 14 hours is the time zone offset to use for comparison.
protected static String
WRONGLY_PLACED
Convenience String for complaints.

Method Summary

void
addDuration(Duration duration)
Adds a Duration to this Date/Time type as defined in Adding Duration to dateTimes (W3C XML Schema, part 2 appendix E). This version uses the algorithm defined in the document from W3C.
protected void
appendDateString(StringBuffer result)
protected void
appendTimeString(StringBuffer result)
protected void
appendTimeZoneString(StringBuffer result)
DateTimeBase
clone(DateTimeBase dateTime)
int
compareTo(DateTimeBase dateTime)
Compares two date/time data types.
protected boolean
equal(DateTimeBase dateTime)
Returns true if the present instance of date/time type is equal to the parameter.
boolean
equals(Object object)
Overrides the java.lang.Object#equals method.
short
getCentury()
short
getDay()
short
getHour()
short
getMilli()
short
getMinute()
short
getMonth()
short
getSeconds()
abstract short[]
getValues()
returns an array of short with all the fields that describe a date/time type.
short
getYear()
short
getZoneHour()
short
getZoneMinute()
boolean
hasCentury()
boolean
hasDay()
boolean
hasHour()
boolean
hasIsNegative()
boolean
hasMilli()
boolean
hasMinute()
boolean
hasMonth()
boolean
hasSeconds()
boolean
hasYear()
int
hashCode()
Overrides the java.lang.Object#hashcode method.
boolean
isLeap(int year)
Returns true if the given year represents a leap year.
boolean
isNegative()
boolean
isUTC()
Returns true if this date/time type is UTC, that is, has a time zone assigned.
boolean
isZoneNegative()
void
normalize()
Normalizes a date/time datatype as defined in W3C XML Schema Recommendation document: if a timeZone is present but it is not Z then we convert the date/time datatype to Z using the addition operation defined in Adding Duration to dateTimes (W3C XML Schema, part 2 appendix E).
protected static int
parseDay(String str, DateTimeBase result, char[] chars, int index, String complaint)
protected static int
parseMonth(String str, DateTimeBase result, char[] chars, int index, String complaint)
protected static int
parseTime(String str, DateTimeBase result, char[] chars, int index, String complaint)
protected static int
parseTimeZone(String str, DateTimeBase result, char[] chars, int index, String complaint)
protected static int
parseYear(String str, DateTimeBase result, char[] chars, int index, String complaint)
void
setCentury(short century)
Set the century field.
protected void
setDateFormatTimeZone(Calendar calendar)
Sets the time zone in the provided Calendar.
protected void
setDateFormatTimeZone(DateFormat df)
Sets the time zone in the provided DateFormat.
void
setDay(short day)
Sets the Day Field.
void
setHour(short hour)
Sets the hour field for this date/time type.
void
setMilliSecond(short millisecond)
Sets the millisecond field for this date/time type.
void
setMinute(short minute)
set the minute field for this date/time type.
void
setMonth(short month)
Sets the Month Field.
void
setNegative()
Set the negative field to true.
void
setSecond(short second)
Sets the seconds field for this date/time type, not including the fractional seconds.
void
setSecond(short second, short millsecond)
Sets the seconds field for this date/time type, including fractional seconds.
void
setUTC()
Sets the UTC field.
abstract void
setValues(short[] values)
Sets all the fields by reading the values in an array.
void
setYear(short year)
Sets the Year field.
void
setZone(short hour, short minute)
Sets the time zone fields for this date/time type.
void
setZoneHour(short hour)
Sets the time zone hour field for this date/time type.
void
setZoneMinute(short minute)
Sets the time zone minute field for this date/time type.
void
setZoneNegative(boolean zoneNegative)
Sets the time zone negative field to true.
Calendar
toCalendar()
converts this Date/Time into a local java Calendar.
abstract Date
toDate()
Returns a java.util.Date that represents the XML Schema Date datatype.

Field Details

EQUALS

public static final int EQUALS
Public constant referring to a Date/Time comparison result of "equals".
Field Value:
1

GREATER_THAN

public static final int GREATER_THAN
Public constant referring to a Date/Time comparison result of "greater than".
Field Value:
2

INDETERMINATE

public static final int INDETERMINATE
Public constant referring to an indeterminate Date/Time comparison.
Field Value:
-1

LESS_THAN

public static final int LESS_THAN
Public constant referring to a Date/Time comparison result of "less than".
Field Value:
0

MAX_TIME_ZONE_COMPARISON_OFFSET

protected static final int MAX_TIME_ZONE_COMPARISON_OFFSET
When comparing a date/time with a time zone to one without, the recommendation says that 14 hours is the time zone offset to use for comparison.
Field Value:
14

WRONGLY_PLACED

protected static final String WRONGLY_PLACED
Convenience String for complaints.

Method Details

addDuration

public void addDuration(Duration duration)
Adds a Duration to this Date/Time type as defined in Adding Duration to dateTimes (W3C XML Schema, part 2 appendix E). This version uses the algorithm defined in the document from W3C. A later version may optimize it.

The modified Date/Time instance will keep the same time zone it started with, if any.

Don't use getter methods but use direct field access for dateTime in order to have the behaviour defined in the Recommendation document.

Parameters:
duration - the duration to add

appendDateString

protected void appendDateString(StringBuffer result)

appendTimeString

protected void appendTimeString(StringBuffer result)

appendTimeZoneString

protected void appendTimeZoneString(StringBuffer result)

clone

public DateTimeBase clone(DateTimeBase dateTime)
            throws CloneNotSupportedException

compareTo

public int compareTo(DateTimeBase dateTime)
Compares two date/time data types. The algorithm of comparison is defined in W3C XML Schema Recommendation (section 3.2.7.3)

The returned value will be one of:

  • INDETERMINATE (-1): this ? dateTime
  • LESS_THAN (0): this <32dateTime
  • EQUALS (1): this == dateTime
  • GREATER_THAN (2): this > dateTime

FIXME: This code does not compare time zones properly for date/time types that do not contain a time.

Parameters:
dateTime - the dateTime to compare with the current instance.
Returns:
the status of the comparison.

equal

protected boolean equal(DateTimeBase dateTime)
Returns true if the present instance of date/time type is equal to the parameter.

The equals relation is as defined in the W3C XML Schema Recommendation, part2.

Parameters:
dateTime - the date/time type to compare with the present instance
Returns:
true if the present instance is equal to the parameter false if not

equals

public boolean equals(Object object)
Overrides the java.lang.Object#equals method.

getCentury

public short getCentury()
            throws UnsupportedOperationException

getDay

public short getDay()
            throws UnsupportedOperationException

getHour

public short getHour()
            throws UnsupportedOperationException

getMilli

public short getMilli()
            throws UnsupportedOperationException

getMinute

public short getMinute()
            throws UnsupportedOperationException

getMonth

public short getMonth()
            throws UnsupportedOperationException

getSeconds

public short getSeconds()
            throws UnsupportedOperationException

getValues

public abstract short[] getValues()
returns an array of short with all the fields that describe a date/time type.
Returns:
an array of short with all the fields that describe a date/time type.

getYear

public short getYear()
            throws UnsupportedOperationException

getZoneHour

public short getZoneHour()

getZoneMinute

public short getZoneMinute()

hasCentury

public boolean hasCentury()

hasDay

public boolean hasDay()

hasHour

public boolean hasHour()

hasIsNegative

public boolean hasIsNegative()

hasMilli

public boolean hasMilli()

hasMinute

public boolean hasMinute()

hasMonth

public boolean hasMonth()

hasSeconds

public boolean hasSeconds()

hasYear

public boolean hasYear()

hashCode

public int hashCode()
Overrides the java.lang.Object#hashcode method.

isLeap

public final boolean isLeap(int year)
Returns true if the given year represents a leap year. A specific year is a leap year if it is either evenly divisible by 400 OR evenly divisible by 4 and not evenly divisible by 100.
Parameters:
year - the year to test where 0 <32year <= 9999
Returns:
true if the given year represents a leap year

isNegative

public boolean isNegative()
            throws UnsupportedOperationException

isUTC

public boolean isUTC()
Returns true if this date/time type is UTC, that is, has a time zone assigned. A date/time type is UTC if a 'Z' appears at the end of the lexical representation type or if it contains a time zone.
Returns:
true if this type has a time zone assigned, else false.

isZoneNegative

public boolean isZoneNegative()

normalize

public void normalize()

parseDay

protected static int parseDay(String str,
                              DateTimeBase result,
                              char[] chars,
                              int index,
                              String complaint)
            throws ParseException

parseMonth

protected static int parseMonth(String str,
                                DateTimeBase result,
                                char[] chars,
                                int index,
                                String complaint)
            throws ParseException

parseTime

protected static int parseTime(String str,
                               DateTimeBase result,
                               char[] chars,
                               int index,
                               String complaint)
            throws ParseException

parseTimeZone

protected static int parseTimeZone(String str,
                                   DateTimeBase result,
                                   char[] chars,
                                   int index,
                                   String complaint)
            throws ParseException

parseYear

protected static int parseYear(String str,
                               DateTimeBase result,
                               char[] chars,
                               int index,
                               String complaint)
            throws ParseException

setCentury

public void setCentury(short century)
            throws UnsupportedOperationException
Set the century field. Note: year 0000 is not allowed.
Parameters:
century - the value to set

setDateFormatTimeZone

protected void setDateFormatTimeZone(Calendar calendar)
Sets the time zone in the provided Calendar.
Parameters:
calendar -

setDateFormatTimeZone

protected void setDateFormatTimeZone(DateFormat df)
Sets the time zone in the provided DateFormat.
Parameters:
df -

setDay

public void setDay(short day)
            throws UnsupportedOperationException
Sets the Day Field. Note: This field is validated before the assignment is done.
Parameters:
day - the value to set up

setHour

public void setHour(short hour)
            throws UnsupportedOperationException
Sets the hour field for this date/time type.
Parameters:
hour - the hour to set

setMilliSecond

public void setMilliSecond(short millisecond)
            throws UnsupportedOperationException
Sets the millisecond field for this date/time type.
Parameters:
millisecond - the millisecond to set

setMinute

public void setMinute(short minute)
            throws UnsupportedOperationException
set the minute field for this date/time type.
Parameters:
minute - the minute to set.

setMonth

public void setMonth(short month)
            throws UnsupportedOperationException
Sets the Month Field. Note 1 <= month <= 12.
Parameters:
month - the value to set up

setNegative

public void setNegative()
            throws UnsupportedOperationException
Set the negative field to true.

setSecond

public void setSecond(short second)
            throws UnsupportedOperationException
Sets the seconds field for this date/time type, not including the fractional seconds. Any fractional seconds previously set is unmodified.
Parameters:
second - the second to set

setSecond

public void setSecond(short second,
                      short millsecond)
            throws UnsupportedOperationException
Sets the seconds field for this date/time type, including fractional seconds. (In this implementation, fractional seconds are limited to milliseconds and are truncated at millseconds if more precision is provided.)
Parameters:
second - the second to set
millsecond - the millisecond to set

setUTC

public void setUTC()
Sets the UTC field.

setValues

public abstract void setValues(short[] values)
Sets all the fields by reading the values in an array.
Parameters:
values - an array of shorts with the values.

setYear

public void setYear(short year)
            throws UnsupportedOperationException
Sets the Year field. Note: year 0000 is not allowed.
Parameters:
year - the year to set

setZone

public void setZone(short hour,
                    short minute)
Parameters:
hour - The time zone hour to set. Must be positive.
minute - The time zone minute to set.

setZoneHour

public void setZoneHour(short hour)
Parameters:
hour - the time zone hour to set. Must be positive.

setZoneMinute

public void setZoneMinute(short minute)
Sets the time zone minute field for this date/time type. A call to this method means that the date/time type used is UTC.
Parameters:
minute - the time zone minute to set

setZoneNegative

public void setZoneNegative(boolean zoneNegative)
Sets the time zone negative field to true.
Parameters:
zoneNegative - indicates whether or not the time zone is negative.

toCalendar

public Calendar toCalendar()
converts this Date/Time into a local java Calendar.
Returns:
a local calendar representing this Date or Time

toDate

public abstract Date toDate()
Returns a java.util.Date that represents the XML Schema Date datatype.
Returns:
a java.util.Date that represents the XML Schema Date datatype.

Intalio Inc. (C) 1999-2008. All rights reserved http://www.intalio.com