javax.xml.datatype

Class XMLGregorianCalendar

Implemented Interfaces:
Cloneable

public abstract class XMLGregorianCalendar
extends java.lang.Object
implements Cloneable

Representation for W3C XML Schema 1.0 date/time datatypes. Specifically, these date/time datatypes are dateTime, time, date, gYearMonth, gMonthDay, gYear gMonth and gDay defined in the XML Namespace "http://www.w3.org/2001/XMLSchema". These datatypes are normatively defined in W3C XML Schema 1.0 Part 2, Section 3.2.7-14.

The table below defines the mapping between XML Schema 1.0 date/time datatype fields and this class' fields. It also summarizes the value constraints for the date and time fields defined in W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats.

Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation
XML Schema 1.0
datatype
field
Related
XMLGregorianCalendar
Accessor(s)
Value Range
year getYear() + getEon() or
getEonAndYear()
getYear() is a value between -(10^9-1) to (10^9)-1 or DatatypeConstants.FIELD_UNDEFINED.
getEon() is high order year value in billion of years.
getEon() has values greater than or equal to (10^9) or less than or equal to -(10^9). A value of null indicates field is undefined. Given that XML Schema 1.0 errata states that the year zero will be a valid lexical value in a future version of XML Schema, this class allows the year field to be set to zero. Otherwise, the year field value is handled exactly as described in the errata and [ISO-8601-1988]. Note that W3C XML Schema 1.0 validation does not allow for the year field to have a value of zero.
month getMonth() 1 to 12 or DatatypeConstants.FIELD_UNDEFINED
day getDay() Independent of month, max range is 1 to 31 or DatatypeConstants.FIELD_UNDEFINED.
The normative value constraint stated relative to month field's value is in W3C XML Schema 1.0 Part 2, Appendix D.
hourgetHour() 0 to 24 or DatatypeConstants.FIELD_UNDEFINED. For a value of 24, the minute and second field must be zero per XML Schema Errata.
minute getMinute() 0 to 59 or DatatypeConstants.FIELD_UNDEFINED
second getSecond() + getMillisecond()/1000 or
getSecond() + getFractionalSecond()
getSecond() from 0 to 60 or DatatypeConstants.FIELD_UNDEFINED.
(Note: 60 only allowable for leap second.)
getFractionalSecond() allows for infinite precision over the range from 0.0 to 1.0 when the getSecond() is defined.
FractionalSecond is optional and has a value of null when it is undefined.
getMillisecond() is the convenience millisecond precision of value of getFractionalSecond().
timezone getTimezone() Number of minutes or DatatypeConstants.FIELD_UNDEFINED. Value range from -14 hours (-14 * 60 minutes) to 14 hours (14 * 60 minutes).

All maximum value space constraints listed for the fields in the table above are checked by factory methods, @{link DatatypeFactory}, setter methods and parse methods of this class. IllegalArgumentException is thrown when a parameter's value is outside the value constraint for the field or if the composite values constitute an invalid XMLGregorianCalendar instance (for example, if the 31st of June is specified).

The following operations are defined for this class:

Version:
$Revision: 226183 $, $Date: 2005-04-08 12:39:14 +0200 (Fri, 08 Apr 2005) $
Authors:
Joseph Fialli
Kohsuke Kawaguchi
Jeff Suttor
Since:
1.5
See Also:
Duration, DatatypeFactory

Method Summary

abstract void
add(Duration duration)
Add duration to this instance.
abstract void
clear()
Unset all fields to undefined.
abstract Object
clone()
Creates and returns a copy of this object.
abstract int
compare(XMLGregorianCalendar xmlGregorianCalendar)
Compare two instances of W3C XML Schema 1.0 date/time datatypes according to partial order relation defined in W3C XML Schema 1.0 Part 2, Section 3.2.7.3, Order relation on dateTime.
boolean
equals(Object obj)
Indicates whether parameter obj is "equal to" this one.
abstract int
getDay()
Return day in month or DatatypeConstants.FIELD_UNDEFINED.
abstract BigInteger
getEon()
Return high order component for XML Schema 1.0 dateTime datatype field for year.
abstract BigInteger
getEonAndYear()
Return XML Schema 1.0 dateTime datatype field for year.
abstract BigDecimal
getFractionalSecond()
Return fractional seconds.
abstract int
getHour()
Return hours or DatatypeConstants.FIELD_UNDEFINED.
int
getMillisecond()
Return millisecond precision of getFractionalSecond().
abstract int
getMinute()
Return minutes or DatatypeConstants.FIELD_UNDEFINED.
abstract int
getMonth()
Return number of month or DatatypeConstants.FIELD_UNDEFINED.
abstract int
getSecond()
Return seconds or DatatypeConstants.FIELD_UNDEFINED.
abstract TimeZone
getTimeZone(int defaultZoneoffset)
Returns a java.util.TimeZone for this class.
abstract int
getTimezone()
Return timezone offset in minutes or DatatypeConstants.FIELD_UNDEFINED if this optional field is not defined.
abstract QName
getXMLSchemaType()
Return the name of the XML Schema date/time type that this instance maps to.
abstract int
getYear()
Return low order component for XML Schema 1.0 dateTime datatype field for year or DatatypeConstants.FIELD_UNDEFINED.
int
hashCode()
Returns a hash code consistent with the definition of the equals method.
abstract boolean
isValid()
Validate instance by getXMLSchemaType() constraints.
abstract XMLGregorianCalendar
normalize()
Normalize this instance to UTC.
abstract void
reset()
Reset this XMLGregorianCalendar to its original values.
abstract void
setDay(int day)
Set days in month.
abstract void
setFractionalSecond(BigDecimal fractional)
Set fractional seconds.
abstract void
setHour(int hour)
Set hours.
abstract void
setMillisecond(int millisecond)
Set milliseconds.
abstract void
setMinute(int minute)
Set minutes.
abstract void
setMonth(int month)
Set month.
abstract void
setSecond(int second)
Set seconds.
void
setTime(int hour, int minute, int second)
Set time as one unit.
void
setTime(int hour, int minute, int second, BigDecimal fractional)
Set time as one unit, including the optional infinite precision fractional seconds.
void
setTime(int hour, int minute, int second, int millisecond)
Set time as one unit, including optional milliseconds.
abstract void
setTimezone(int offset)
Set the number of minutes in the timezone offset.
abstract void
setYear(BigInteger year)
Set low and high order component of XSD dateTime year field.
abstract void
setYear(int year)
Set year of XSD dateTime year field.
abstract GregorianCalendar
toGregorianCalendar()
Convert this XMLGregorianCalendar to a GregorianCalendar.
abstract GregorianCalendar
toGregorianCalendar(java.util.TimeZone timezone, java.util.Locale aLocale, XMLGregorianCalendar defaults)
Convert this XMLGregorianCalendar along with provided parameters to a GregorianCalendar instance.
String
toString()
Returns a String representation of this XMLGregorianCalendar Object.
abstract String
toXMLFormat()
Return the lexical representation of this instance.

Method Details

add

public abstract void add(Duration duration)
Add duration to this instance.

The computation is specified in XML Schema 1.0 Part 2, Appendix E, Adding durations to dateTimes>. date/time field mapping table defines the mapping from XML Schema 1.0 dateTime fields to this class' representation of those fields.

Parameters:
duration - Duration to add to this XMLGregorianCalendar.

clear

public abstract void clear()

clone

public abstract Object clone()
Creates and returns a copy of this object.
Returns:
copy of this Object

compare

public abstract int compare(XMLGregorianCalendar xmlGregorianCalendar)
Compare two instances of W3C XML Schema 1.0 date/time datatypes according to partial order relation defined in W3C XML Schema 1.0 Part 2, Section 3.2.7.3, Order relation on dateTime.

xsd:dateTime datatype field mapping to accessors of this class are defined in date/time field mapping table.

Parameters:
xmlGregorianCalendar - Instance of XMLGregorianCalendar to compare
Returns:
The relationship between this XMLGregorianCalendar and the specified xmlGregorianCalendar as DatatypeConstants.LESSER, DatatypeConstants.EQUAL, DatatypeConstants.GREATER or DatatypeConstants.INDETERMINATE.

equals

public boolean equals(Object obj)
Indicates whether parameter obj is "equal to" this one.
Parameters:
obj - to compare.

getDay

public abstract int getDay()
See Also:
setDay(int)

getEon

public abstract BigInteger getEon()
Returns:
eon of this XMLGregorianCalendar. The value returned is an integer multiple of 10^9.

getEonAndYear

public abstract BigInteger getEonAndYear()
Returns:
sum of eon and BigInteger.valueOf(year) when both fields are defined. When only year is defined, return it. When both eon and year are not defined, return null.

getFractionalSecond

public abstract BigDecimal getFractionalSecond()
Returns:
fractional seconds of this XMLGregorianCalendar.

getHour

public abstract int getHour()
Return hours or DatatypeConstants.FIELD_UNDEFINED. Returns DatatypeConstants.FIELD_UNDEFINED if this field is not defined.

Value constraints for this value are summarized in hour field of date/time field mapping table.


getMillisecond

public int getMillisecond()
Return millisecond precision of getFractionalSecond().

This method represents a convenience accessor to infinite precision fractional second value returned by getFractionalSecond(). The returned value is the rounded down to milliseconds value of getFractionalSecond(). When getFractionalSecond() returns null, this method must return DatatypeConstants.FIELD_UNDEFINED.

Value constraints for this value are summarized in second field of date/time field mapping table.

Returns:
Millisecond of this XMLGregorianCalendar.

getMinute

public abstract int getMinute()
Return minutes or DatatypeConstants.FIELD_UNDEFINED. Returns DatatypeConstants.FIELD_UNDEFINED if this field is not defined.

Value constraints for this value are summarized in minute field of date/time field mapping table.


getMonth

public abstract int getMonth()
Returns:
year of this XMLGregorianCalendar.

getSecond

public abstract int getSecond()
Return seconds or DatatypeConstants.FIELD_UNDEFINED.

Returns DatatypeConstants.FIELD_UNDEFINED if this field is not defined. When this field is not defined, the optional xs:dateTime fractional seconds field, represented by getFractionalSecond() and getMillisecond(), must not be defined.

Value constraints for this value are summarized in second field of date/time field mapping table.

Returns:
Second of this XMLGregorianCalendar.

getTimeZone

public abstract TimeZone getTimeZone(int defaultZoneoffset)
Returns a java.util.TimeZone for this class.

If timezone field is defined for this instance, returns TimeZone initialized with custom timezone id of zoneoffset. If timezone field is undefined, try the defaultZoneoffset that was passed in. If defaultZoneoffset is FIELD_UNDEFINED, return default timezone for this host. (Same default as java.util.GregorianCalendar).

Returns:
TimeZone for this.

getTimezone

public abstract int getTimezone()
Return timezone offset in minutes or DatatypeConstants.FIELD_UNDEFINED if this optional field is not defined.

Value constraints for this value are summarized in timezone field of date/time field mapping table.


getXMLSchemaType

public abstract QName getXMLSchemaType()
Return the name of the XML Schema date/time type that this instance maps to. Type is computed based on fields that are set.
Required fields for XML Schema 1.0 Date/Time Datatypes.
(timezone is optional for all date/time datatypes)
Datatypeyearmonthdayhourminutesecond
DatatypeConstants.DATETIMEXXXXXX
DatatypeConstants.DATEXXX
DatatypeConstants.TIMEXXX
DatatypeConstants.GYEARMONTHXX
DatatypeConstants.GMONTHDAYXX
DatatypeConstants.GYEARX
DatatypeConstants.GMONTHX
DatatypeConstants.GDAYX

getYear

public abstract int getYear()
Returns:
year of this XMLGregorianCalendar.

hashCode

public int hashCode()
Returns a hash code consistent with the definition of the equals method.
Returns:
hash code of this object.

isValid

public abstract boolean isValid()
Validate instance by getXMLSchemaType() constraints.
Returns:
true if data values are valid.

normalize

public abstract XMLGregorianCalendar normalize()
Normalize this instance to UTC.

2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z

Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).

Returns:
this XMLGregorianCalendar normalized to UTC.

reset

public abstract void reset()
Reset this XMLGregorianCalendar to its original values.

XMLGregorianCalendar is reset to the same values as when it was created with DatatypeFactory.newXMLGregorianCalendar(), DatatypeFactory.newXMLGregorianCalendar(String lexicalRepresentation), DatatypeFactory.newXMLGregorianCalendar( BigInteger year, int month, int day, int hour, int minute, int second, BigDecimal fractionalSecond, int timezone), DatatypeFactory.newXMLGregorianCalendar( int year, int month, int day, int hour, int minute, int second, int millisecond, int timezone), DatatypeFactory.newXMLGregorianCalendar(GregorianCalendar cal), DatatypeFactory.newXMLGregorianCalendarDate( int year, int month, int day, int timezone), DatatypeFactory.newXMLGregorianCalendarTime( int hours, int minutes, int seconds, int timezone), DatatypeFactory.newXMLGregorianCalendarTime( int hours, int minutes, int seconds, BigDecimal fractionalSecond, int timezone) or DatatypeFactory.newXMLGregorianCalendarTime( int hours, int minutes, int seconds, int milliseconds, int timezone).

reset() is designed to allow the reuse of existing XMLGregorianCalendars thus saving resources associated with the creation of new XMLGregorianCalendars.


setDay

public abstract void setDay(int day)
Parameters:
day - value constraints summarized in day field of date/time field mapping table.

setFractionalSecond

public abstract void setFractionalSecond(BigDecimal fractional)
Set fractional seconds.

Unset this field by invoking the setter with a parameter value of null.


setHour

public abstract void setHour(int hour)
Parameters:
hour - value constraints summarized in hour field of date/time field mapping table.

setMillisecond

public abstract void setMillisecond(int millisecond)
Parameters:
millisecond - value constraints summarized in millisecond field of date/time field mapping table.

setMinute

public abstract void setMinute(int minute)
Parameters:
minute - value constraints summarized in minute field of date/time field mapping table.

setMonth

public abstract void setMonth(int month)
Parameters:
month - value constraints summarized in month field of date/time field mapping table.

setSecond

public abstract void setSecond(int second)
Parameters:
second - value constraints summarized in second field of date/time field mapping table.

setTime

public void setTime(int hour,
                    int minute,
                    int second)
Set time as one unit.

setTime

public void setTime(int hour,
                    int minute,
                    int second,
                    BigDecimal fractional)
Set time as one unit, including the optional infinite precision fractional seconds.
Parameters:
hour - value constraints are summarized in hour field of date/time field mapping table.
minute - value constraints are summarized in minute field of date/time field mapping table.
second - value constraints are summarized in second field of date/time field mapping table.
fractional - value of null indicates this optional field is not set.

setTime

public void setTime(int hour,
                    int minute,
                    int second,
                    int millisecond)
Set time as one unit, including optional milliseconds.
Parameters:
hour - value constraints are summarized in hour field of date/time field mapping table.
minute - value constraints are summarized in minute field of date/time field mapping table.
second - value constraints are summarized in second field of date/time field mapping table.
millisecond - value of DatatypeConstants.FIELD_UNDEFINED indicates this optional field is not set.

setTimezone

public abstract void setTimezone(int offset)
Parameters:
offset - value constraints summarized in timezone field of date/time field mapping table.

setYear

public abstract void setYear(BigInteger year)
Set low and high order component of XSD dateTime year field.

Unset this field by invoking the setter with a parameter value of null.


setYear

public abstract void setYear(int year)
Set year of XSD dateTime year field.

Unset this field by invoking the setter with a parameter value of DatatypeConstants.FIELD_UNDEFINED.

Note: if the absolute value of the year parameter is less than 10^9, the eon component of the XSD year field is set to null by this method.

Parameters:
year - value constraints are summarized in year field of date/time field mapping table. If year is DatatypeConstants.FIELD_UNDEFINED, then eon is set to null.

toGregorianCalendar

public abstract GregorianCalendar toGregorianCalendar()
Convert this XMLGregorianCalendar to a GregorianCalendar.

When this instance has an undefined field, this conversion relies on the java.util.GregorianCalendar default for its corresponding field. A notable difference between XML Schema 1.0 date/time datatypes and java.util.GregorianCalendar is that Timezone value is optional for date/time datatypes and it is a required field for java.util.GregorianCalendar. See javadoc for java.util.TimeZone.getDefault() on how the default is determined. To explicitly specify the TimeZone instance, see toGregorianCalendar(TimeZone, Locale, XMLGregorianCalendar).

Field by Field Conversion from this class to java.util.GregorianCalendar
java.util.GregorianCalendar fieldjavax.xml.datatype.XMLGregorianCalendar field
ERAgetEonAndYear().signum() <320 ? GregorianCalendar.BC : GregorianCalendar.AD
YEARgetEonAndYear().abs().intValue()*
MONTHgetMonth() - DatatypeConstants.JANUARY + GregorianCalendar.JANUARY
DAY_OF_MONTHgetDay()
HOUR_OF_DAYgetHour()
MINUTEgetMinute()
SECONDgetSecond()
MILLISECONDget millisecond order from getFractionalSecond()*
GregorianCalendar.setTimeZone(TimeZone)getTimezone() formatted into Custom timezone id
* designates possible loss of precision during the conversion due to source datatype having higher precision than target datatype.

To ensure consistency in conversion implementations, the new GregorianCalendar should be instantiated in following manner.

  • Using timeZone value as defined above, create a new java.util.GregorianCalendar(timeZone,Locale.getDefault()).
  • Initialize all GregorianCalendar fields by calling {(@link GegorianCalendar#clear()}.
  • Obtain a pure Gregorian Calendar by invoking GregorianCalendar.setGregorianChange( new Date(Long.MIN_VALUE)).
  • Its fields ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, SECOND and MILLISECOND are set using the method Calendar.set(int,int)

toGregorianCalendar

public abstract GregorianCalendar toGregorianCalendar(java.util.TimeZone timezone,
                                                      java.util.Locale aLocale,
                                                      XMLGregorianCalendar defaults)
Convert this XMLGregorianCalendar along with provided parameters to a GregorianCalendar instance.

Since XML Schema 1.0 date/time datetypes has no concept of timezone ids or daylight savings timezone ids, this conversion operation allows the user to explicitly specify one with timezone parameter.

To compute the return value's TimeZone field,

  • when parameter timeZone is non-null, it is the timezone field.
  • else when this.getTimezone() != FIELD_UNDEFINED, create a java.util.TimeZone with a custom timezone id using the this.getTimezone().
  • else when defaults.getTimezone() != FIELD_UNDEFINED, create a java.util.TimeZone with a custom timezone id using defaults.getTimezone().
  • else use the GregorianCalendar default timezone value for the host is defined as specified by java.util.TimeZone.getDefault().
  • GregorianCalendar

  • Create a new java.util.GregorianCalendar(TimeZone, Locale) with TimeZone set as specified above and the Locale parameter.
  • Initialize all GregorianCalendar fields by calling GregorianCalendar.clear()
  • Obtain a pure Gregorian Calendar by invoking GregorianCalendar.setGregorianChange( new Date(Long.MIN_VALUE)).
  • Its fields ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, SECOND and MILLISECOND are set using the method Calendar.set(int,int)
Parameters:
timezone - provide Timezone. null is a legal value.
aLocale - provide explicit Locale. Use default GregorianCalendar locale if value is null.
defaults - provide default field values to use when corresponding field for this instance is FIELD_UNDEFINED or null. If defaultsis null or a field within the specified defaults is undefined, just use java.util.GregorianCalendar defaults.
Returns:
a java.util.GregorianCalendar conversion of this instance.

toString

public String toString()
Returns:
A non-null valid String representation of this XMLGregorianCalendar.
See Also:
toXMLFormat()

toXMLFormat

public abstract String toXMLFormat()
Returns:
XML, as String, representation of this XMLGregorianCalendar