net.sf.saxon.xqj

Class SaxonXMLGregorianCalendar


public class SaxonXMLGregorianCalendar
extends XMLGregorianCalendar

Saxon implementation of the JAXP class javax.xml.datatype.XMLGregorianCalendar. This is currently used only by the XQJ interface for XQuery: the normal representation of a date, time, or dateTime value in Saxon is with a subclass of CalendarValue

The JAXP specification for this class defines it in terms of XML Schema 1.0 semantics. This implementation is more aligned to the XPath 2.0 semantics of the data types.

Note that this class, unlike the representations of all other data types, is mutable.

Constructor Summary

SaxonXMLGregorianCalendar(CalendarValue value)
Create a SaxonXMLGregorianCalendar from a Saxon CalendarValue object

Method Summary

void
add(Duration duration)
Add duration to this instance.
void
clear()
Unset all fields to undefined.
Object
clone()
Creates and returns a copy of this object.
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.
int
getDay()
Return day in month or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
BigInteger
getEon()
Return high order component for XML Schema 1.0 dateTime datatype field for year.
BigInteger
getEonAndYear()
Return XML Schema 1.0 dateTime datatype field for year.
BigDecimal
getFractionalSecond()
Return fractional seconds.
int
getHour()
Return hours or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
int
getMicrosecond()
Return microsecond precision of getFractionalSecond().
int
getMinute()
Return minutes or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
int
getMonth()
Return number of month or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
int
getSecond()
Return seconds or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
TimeZone
getTimeZone(int defaultZoneoffset)
Returns a java.util.TimeZone for this class.
int
getTimezone()
Return timezone offset in minutes or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED if this optional field is not defined.
QName
getXMLSchemaType()
Return the name of the XML Schema date/time type that this instance maps to.
int
getYear()
Return low order component for XML Schema 1.0 dateTime datatype field for year or javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
boolean
isValid()
Validate instance by getXMLSchemaType() constraints.
XMLGregorianCalendar
normalize()
Normalize this instance to UTC.
void
reset()
Reset this XMLGregorianCalendar to its original values.
void
setCalendarValue(CalendarValue value)
Set the calendar value of this object
void
setDay(int day)
Set days in month.
void
setFractionalSecond(BigDecimal fractional)
Set fractional seconds.
void
setHour(int hour)
Set hours.
void
setMillisecond(int millisecond)
Set milliseconds.
void
setMinute(int minute)
Set minutes.
void
setMonth(int month)
Set month.
void
setSecond(int second)
Set seconds.
void
setTimezone(int offset)
Set the number of minutes in the timezone offset.
void
setYear(BigInteger year)
Set low and high order component of XSD dateTime year field.
void
setYear(int year)
Set year of XSD dateTime year field.
CalendarValue
toCalendarValue()
Convert this SaxonXMLGregorianCalendar to a Saxon CalendarValue object
GregorianCalendar
toGregorianCalendar()
Convert this XMLGregorianCalendar to a java.util.GregorianCalendar.
GregorianCalendar
toGregorianCalendar(TimeZone timezone, Locale aLocale, XMLGregorianCalendar defaults)
Convert this XMLGregorianCalendar along with provided parameters to a java.util.GregorianCalendar instance.
String
toXMLFormat()
Return the lexical representation of this instance.

Constructor Details

SaxonXMLGregorianCalendar

public SaxonXMLGregorianCalendar(CalendarValue value)
Create a SaxonXMLGregorianCalendar from a Saxon CalendarValue object
Parameters:
value - the CalendarValue

Method Details

add

public 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 void clear()
Unset all fields to undefined.

Set all int fields to javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED and reference fields to null.


clone

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

compare

public int compare(XMLGregorianCalendar xmlGregorianCalendar)
Parameters:
xmlGregorianCalendar - Instance of XMLGregorianCalendar to compare
Returns:
The relationship between this XMLGregorianCalendar and the specified xmlGregorianCalendar as javax.xml.datatype.DatatypeConstants.LESSER, javax.xml.datatype.DatatypeConstants.EQUAL, javax.xml.datatype.DatatypeConstants.GREATER or javax.xml.datatype.DatatypeConstants.INDETERMINATE.

getDay

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

getEon

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

getEonAndYear

public 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 BigDecimal getFractionalSecond()
Return fractional seconds.

null is returned when this optional field is not defined.

Value constraints are detailed in second field of date/time field mapping table.

This optional field can only have a defined value when the xs:dateTime second field, represented by getSecond(), does not return javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.

Returns:
fractional seconds of this XMLGregorianCalendar.
See Also:
getSecond(), setTime(int, int, int, java.math.BigDecimal)

getHour

public int getHour()
See Also:
setTime(int, int, int)

getMicrosecond

public int getMicrosecond()
Return microsecond 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 microseconds 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.
See Also:
getFractionalSecond(), setTime(int, int, int)

getMinute

public int getMinute()
See Also:
setTime(int, int, int)

getMonth

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

getSecond

public int getSecond()
Returns:
Second of this XMLGregorianCalendar.
See Also:
getFractionalSecond(), getMillisecond(), setTime(int, int, int)

getTimeZone

public 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).

Parameters:
defaultZoneoffset - default zoneoffset if this zoneoffset is javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED.
Returns:
TimeZone for this.

getTimezone

public int getTimezone()

getXMLSchemaType

public 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.
Returns:
One of the following class constants: javax.xml.datatype.DatatypeConstants.DATETIME, javax.xml.datatype.DatatypeConstants.TIME, javax.xml.datatype.DatatypeConstants.DATE, javax.xml.datatype.DatatypeConstants.GYEARMONTH, javax.xml.datatype.DatatypeConstants.GMONTHDAY, javax.xml.datatype.DatatypeConstants.GYEAR, javax.xml.datatype.DatatypeConstants.GMONTH or javax.xml.datatype.DatatypeConstants.GDAY.

getYear

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

isValid

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

normalize

public 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:
a copy of this XMLGregorianCalendar normalized to UTC.

reset

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

setCalendarValue

public void setCalendarValue(CalendarValue value)
Set the calendar value of this object
Parameters:
value - the calendar value

setDay

public void setDay(int day)
Set days in month.

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


setFractionalSecond

public void setFractionalSecond(BigDecimal fractional)
Set fractional seconds.

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


setHour

public void setHour(int hour)
Set hours.

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


setMillisecond

public void setMillisecond(int millisecond)
Set milliseconds.

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


setMinute

public void setMinute(int minute)
Set minutes.

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


setMonth

public void setMonth(int month)
Set month.

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


setSecond

public void setSecond(int second)
Set seconds.

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


setTimezone

public void setTimezone(int offset)
Set the number of minutes in the timezone offset.

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


setYear

public 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 void setYear(int year)
Set year of XSD dateTime year field.

Unset this field by invoking the setter with a parameter value of javax.xml.datatype.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 javax.xml.datatype.DatatypeConstants.FIELD_UNDEFINED, then eon is set to null.

toCalendarValue

public CalendarValue toCalendarValue()
Convert this SaxonXMLGregorianCalendar to a Saxon CalendarValue object
Returns:
the corresponding CalendarValue

toGregorianCalendar

public GregorianCalendar toGregorianCalendar()
Convert this XMLGregorianCalendar to a java.util.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(java.util.TimeZone, Locale, javax.xml.datatype.XMLGregorianCalendar).

See Also:
toGregorianCalendar(java.util.TimeZone, java.util.Locale, javax.xml.datatype.XMLGregorianCalendar)

toGregorianCalendar

public GregorianCalendar toGregorianCalendar(TimeZone timezone,
                                             Locale aLocale,
                                             XMLGregorianCalendar defaults)
Convert this XMLGregorianCalendar along with provided parameters to a java.util.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 java.util.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.

toXMLFormat

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