Class ImmutableXMLGregorianCalendar

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class ImmutableXMLGregorianCalendar
    extends javax.xml.datatype.XMLGregorianCalendar
    An immutable version of the XMLGregorianCalendar.

    The following methods throws IllegalStateException:

    • clear()
    • reset()
    • setYear(BigInteger year)
    • setYear(int year)
    • setMonth(int month)
    • setDay(int day)
    • setTimezone(int offset)
    • setHour(int hour)
    • setMinute(int minute)
    • setSecond(int second)
    • setMillisecond(int millisecond)
    • setFractionalSecond(BigDecimal fractional)
    • add(Duration duration)

    The XMLGregorianCalendar normalize() method returned a new cloned instance that is normalized.

    Author:
    Christopher Mindus
    • Constructor Summary

      Constructors 
      ConstructorDescription
      ImmutableXMLGregorianCalendar​(javax.xml.datatype.XMLGregorianCalendar xc)
      Constructs the immutable XMLGregorianCalendar instance, cloning the parameter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      voidadd​(javax.xml.datatype.Duration duration) 
      voidclear() 
      ImmutableXMLGregorianCalendarclone()
      Clones the instance.
      intcompare​(javax.xml.datatype.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.
      booleanequals​(java.lang.Object obj)
      Compares this calendar to the specified object.
      intgetDay()
      Return day in month or DatatypeConstants.FIELD_UNDEFINED.
      java.math.BigIntegergetEon()
      Return high order component for XML Schema 1.0 dateTime datatype field for year.
      java.math.BigIntegergetEonAndYear()
      Return XML Schema 1.0 dateTime datatype field for year.
      java.math.BigDecimalgetFractionalSecond()
      Return fractional seconds.
      intgetHour()
      Return hours or DatatypeConstants.FIELD_UNDEFINED.
      intgetMillisecond()
      Return millisecond precision of getFractionalSecond().
      intgetMinute()
      Return minutes or DatatypeConstants.FIELD_UNDEFINED.
      intgetMonth()
      Return number of month or DatatypeConstants.FIELD_UNDEFINED.
      intgetSecond()
      Return seconds or DatatypeConstants.FIELD_UNDEFINED.
      intgetTimezone()
      Return timezone offset in minutes or DatatypeConstants.FIELD_UNDEFINED if this optional field is not defined.
      java.util.TimeZonegetTimeZone​(int defaultZoneoffset)
      Returns a java.util.TimeZone for this class.
      javax.xml.namespace.QNamegetXMLSchemaType()
      Return the name of the XML Schema date/time type that this instance maps to.
      intgetYear()
      Return low order component for XML Schema 1.0 dateTime datatype field for year or DatatypeConstants.FIELD_UNDEFINED.
      inthashCode()
      Returns a hash code consistent with the definition of the equals method.
      booleanisValid()
      Validate instance by getXMLSchemaType() constraints.
      javax.xml.datatype.XMLGregorianCalendarnormalize()
      Normalize this instance to UTC.
      voidreset() 
      voidsetDay​(int day) 
      voidsetFractionalSecond​(java.math.BigDecimal fractional) 
      voidsetHour​(int hour) 
      voidsetMillisecond​(int millisecond) 
      voidsetMinute​(int minute) 
      voidsetMonth​(int month) 
      voidsetSecond​(int second) 
      voidsetTimezone​(int offset) 
      voidsetYear​(int year) 
      voidsetYear​(java.math.BigInteger year) 
      java.util.GregorianCalendartoGregorianCalendar()
      Convert this XMLGregorianCalendar to a GregorianCalendar.
      java.util.GregorianCalendartoGregorianCalendar​(java.util.TimeZone timezone, java.util.Locale aLocale, javax.xml.datatype.XMLGregorianCalendar defaults)
      Convert this XMLGregorianCalendar along with provided parameters to a GregorianCalendar instance.
      java.lang.StringtoString()
      Returns a String representation of this XMLGregorianCalendar Object.
      java.lang.StringtoXMLFormat()
      Return the lexical representation of this instance.
      java.lang.StringtoXMLFormat​(javax.xml.namespace.QName datatype)
      Formats the string in XML for the datatype if possible.
      • Methods inherited from class javax.xml.datatype.XMLGregorianCalendar

        setTime, setTime, setTime
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ImmutableXMLGregorianCalendar

        public ImmutableXMLGregorianCalendar​(javax.xml.datatype.XMLGregorianCalendar xc)
        Constructs the immutable XMLGregorianCalendar instance, cloning the parameter.
        Parameters:
        xc - The mutable XMLGregorianCalendar instance.
    • Method Detail

      • clone

        public ImmutableXMLGregorianCalendar clone()
        Clones the instance.
        Specified by:
        clone in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        A cloned instance.
      • getEon

        public java.math.BigInteger getEon()
        Return high order component for XML Schema 1.0 dateTime datatype field for year. null if this optional part of the year field is not defined.

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

        Specified by:
        getEon in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        eon of this XMLGregorianCalendar. The value returned is an integer multiple of 10^9.
        See Also:
        getYear(), getEonAndYear()
      • getYear

        public int getYear()
        Return low order component for XML Schema 1.0 dateTime datatype field for year or DatatypeConstants.FIELD_UNDEFINED.

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

        Specified by:
        getYear in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        year of this XMLGregorianCalendar.
        See Also:
        getEon(), getEonAndYear()
      • getEonAndYear

        public java.math.BigInteger getEonAndYear()
        Return XML Schema 1.0 dateTime datatype field for year.

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

        Specified by:
        getEonAndYear in class javax.xml.datatype.XMLGregorianCalendar
        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.
        See Also:
        getEon(), getYear()
      • getMonth

        public int getMonth()
        Return number of month or DatatypeConstants.FIELD_UNDEFINED.

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

        Specified by:
        getMonth in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        year of this XMLGregorianCalendar.
      • getDay

        public int getDay()
        Return day in month or DatatypeConstants.FIELD_UNDEFINED.

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

        Specified by:
        getDay in class javax.xml.datatype.XMLGregorianCalendar
      • getTimezone

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

        Specified by:
        getTimezone in class javax.xml.datatype.XMLGregorianCalendar
      • getHour

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

        Specified by:
        getHour in class javax.xml.datatype.XMLGregorianCalendar
      • getMinute

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

        Specified by:
        getMinute in class javax.xml.datatype.XMLGregorianCalendar
      • getFractionalSecond

        public java.math.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 DatatypeConstants.FIELD_UNDEFINED.

        Specified by:
        getFractionalSecond in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        fractional seconds of this XMLGregorianCalendar.
        See Also:
        getSecond()
      • compare

        public int compare​(javax.xml.datatype.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.

        Specified by:
        compare in class javax.xml.datatype.XMLGregorianCalendar
        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.
        Throws:
        java.lang.NullPointerException - if xmlGregorianCalendar is null.
      • equals

        public boolean equals​(java.lang.Object obj)
        Compares this calendar to the specified object. The result is true if and only if the argument is not null and is an XMLGregorianCalendar object that represents the same instant in time as this object.
        Overrides:
        equals in class javax.xml.datatype.XMLGregorianCalendar
        Parameters:
        obj - to compare.
        Returns:
        true when obj is an instance of XMLGregorianCalendar and compare(XMLGregorianCalendar obj) returns DatatypeConstants.EQUAL, otherwise false.
      • hashCode

        public int hashCode()
        Returns a hash code consistent with the definition of the equals method.
        Overrides:
        hashCode in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        hash code of this object.
      • toXMLFormat

        public java.lang.String toXMLFormat()
        Return the lexical representation of this instance. The format is specified in XML Schema 1.0 Part 2, Section 3.2.[7-14].1, Lexical Representation".

        Specific target lexical representation format is determined by getXMLSchemaType().

        Specified by:
        toXMLFormat in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        XML, as String, representation of this XMLGregorianCalendar
        Throws:
        java.lang.IllegalStateException - if the combination of set fields does not match one of the eight defined XML Schema builtin date/time datatypes.
      • getXMLSchemaType

        public javax.xml.namespace.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
        Specified by:
        getXMLSchemaType in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        One of the following class constants: DatatypeConstants.DATETIME, DatatypeConstants.TIME, DatatypeConstants.DATE, DatatypeConstants.GYEARMONTH, DatatypeConstants.GMONTHDAY, DatatypeConstants.GYEAR, DatatypeConstants.GMONTH or DatatypeConstants.GDAY.
        Throws:
        java.lang.IllegalStateException - if the combination of set fields does not match one of the eight defined XML Schema builtin date/time datatypes.
      • toString

        public java.lang.String toString()
        Returns a String representation of this XMLGregorianCalendar Object.

        The result is a lexical representation generated by toXMLFormat().

        Overrides:
        toString in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        A non-null valid String representation of this XMLGregorianCalendar.
        Throws:
        java.lang.IllegalStateException - if the combination of set fields does not match one of the eight defined XML Schema builtin date/time datatypes.
        See Also:
        toXMLFormat()
      • isValid

        public boolean isValid()
        Validate instance by getXMLSchemaType() constraints.
        Specified by:
        isValid in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        true if data values are valid.
      • toGregorianCalendar

        public java.util.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() < 0 ? GregorianCalendar.BC : GregorianCalendar.AD
        YEARgetEonAndYear().abs().intValue()*
        MONTHgetMonth() - DatatypeConstants.JANUARY + Calendar.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 Calendar.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)
        Specified by:
        toGregorianCalendar in class javax.xml.datatype.XMLGregorianCalendar
        See Also:
        toGregorianCalendar(java.util.TimeZone, java.util.Locale, XMLGregorianCalendar)
      • toGregorianCalendar

        public java.util.GregorianCalendar toGregorianCalendar​(java.util.TimeZone timezone,
                                                               java.util.Locale aLocale,
                                                               javax.xml.datatype.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().

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

        • Create a new java.util.GregorianCalendar(TimeZone, Locale) with TimeZone set as specified above and the Locale parameter.
        • Initialize all GregorianCalendar fields by calling Calendar.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)
        Specified by:
        toGregorianCalendar in class javax.xml.datatype.XMLGregorianCalendar
        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 defaults is 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.
      • getTimeZone

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

        Specified by:
        getTimeZone in class javax.xml.datatype.XMLGregorianCalendar
        Parameters:
        defaultZoneoffset - default zoneoffset if this zoneoffset is DatatypeConstants.FIELD_UNDEFINED.
        Returns:
        TimeZone for this.
      • toXMLFormat

        public java.lang.String toXMLFormat​(javax.xml.namespace.QName datatype)
                                     throws java.lang.IllegalArgumentException,
                                            java.lang.IllegalStateException
        Formats the string in XML for the datatype if possible.
        Parameters:
        datatype - The XML date/time datatype.
        Throws:
        java.lang.IllegalArgumentException - If the datatype
        java.lang.IllegalStateException - If the combination of set fields does not match one of the eight defined XML Schema built-in date/time datatypes.
      • clear

        public void clear()
        Specified by:
        clear in class javax.xml.datatype.XMLGregorianCalendar
      • reset

        public void reset()
        Specified by:
        reset in class javax.xml.datatype.XMLGregorianCalendar
      • setYear

        public void setYear​(java.math.BigInteger year)
        Specified by:
        setYear in class javax.xml.datatype.XMLGregorianCalendar
      • setYear

        public void setYear​(int year)
        Specified by:
        setYear in class javax.xml.datatype.XMLGregorianCalendar
      • setMonth

        public void setMonth​(int month)
        Specified by:
        setMonth in class javax.xml.datatype.XMLGregorianCalendar
      • setDay

        public void setDay​(int day)
        Specified by:
        setDay in class javax.xml.datatype.XMLGregorianCalendar
      • setTimezone

        public void setTimezone​(int offset)
        Specified by:
        setTimezone in class javax.xml.datatype.XMLGregorianCalendar
      • setHour

        public void setHour​(int hour)
        Specified by:
        setHour in class javax.xml.datatype.XMLGregorianCalendar
      • setMinute

        public void setMinute​(int minute)
        Specified by:
        setMinute in class javax.xml.datatype.XMLGregorianCalendar
      • setSecond

        public void setSecond​(int second)
        Specified by:
        setSecond in class javax.xml.datatype.XMLGregorianCalendar
      • setMillisecond

        public void setMillisecond​(int millisecond)
        Specified by:
        setMillisecond in class javax.xml.datatype.XMLGregorianCalendar
      • setFractionalSecond

        public void setFractionalSecond​(java.math.BigDecimal fractional)
        Specified by:
        setFractionalSecond in class javax.xml.datatype.XMLGregorianCalendar
      • add

        public void add​(javax.xml.datatype.Duration duration)
        Specified by:
        add in class javax.xml.datatype.XMLGregorianCalendar
      • normalize

        public javax.xml.datatype.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).

        Specified by:
        normalize in class javax.xml.datatype.XMLGregorianCalendar
        Returns:
        A clone()'d instance of XMLGregorianCalendar normalized to UTC.