net.sf.saxon.value

## Class DateTimeValue

- java.lang.Object
- - net.sf.saxon.om.AbstractItem
  - - net.sf.saxon.value.AtomicValue
    - - net.sf.saxon.value.CalendarValue
      - - net.sf.saxon.value.DateTimeValue

- All Implemented Interfaces:
    Comparable, PullEvent, AtomicSequence, GroundedValue, IdentityComparable, Item, Sequence, ConversionResult

      public final class DateTimeValue
      extends CalendarValue
      implements Comparable

  A value of type DateTime

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static DateTimeValue` | `EPOCH` Fixed date/time used by Java (and Unix) as the origin of the universe: 1970-01-01

    - ### Fields inherited from class net.sf.saxon.value.CalendarValue

      `NO_TIMEZONE`

    - ### Fields inherited from class net.sf.saxon.value.AtomicValue

      `typeLabel`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DateTimeValue(Calendar calendar, boolean tzSpecified)` Constructor: create a dateTime value given a Java calendar object
    `DateTimeValue(int year, byte month, byte day, byte hour, byte minute, byte second, int microsecond, int tz, boolean xsd10Check)` Constructor: construct a DateTimeValue from its components.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `CalendarValue` | `add(DurationValue duration)` Add a duration to a dateTime
    `CalendarValue` | `adjustTimezone(int timezone)` Return a new dateTime with the same normalized value, but in a different timezone.
    `int` | `compareTo(CalendarValue other, XPathContext context)` Compare the value to another dateTime value, following the XPath comparison semantics
    `int` | `compareTo(Object v2)` Context-free comparison of two DateTimeValue values.
    `ValidationFailure` | `convertToSubType(BuiltInAtomicType subtype)` Convert the value to a built-in subtype of xs:dateTime
    `AtomicValue` | `copyAsSubType(AtomicType typeLabel)` Make a copy of this date, time, or dateTime value, but with a new type label
    `boolean` | `equals(Object o)` Context-free comparison of two dateTime values
    `static DateTimeValue` | `fromJavaDate(Date suppliedDate)` Factory method: create a dateTime value given a Java Date object.
    `static DateTimeValue` | `fromJulianInstant(BigDecimal instant)` Get the DateTimeValue corresponding to a given Julian instant
    `GregorianCalendar` | `getCalendar()` Get a Java Calendar object representing the value of this DateTime.
    `CharSequence` | `getCanonicalLexicalRepresentation()` Get the canonical lexical representation as defined in XML Schema.
    `ComparisonKey` | `getComparisonKey(XPathContext context)` Get a comparison key for this value.
    `AtomicValue` | `getComponent(int component)` Get a component of the value.
    `static DateTimeValue` | `getCurrentDateTime(XPathContext context)` Get the dateTime value representing the nominal date/time of this transformation run.
    `byte` | `getDay()` Get the day component, 1-31
    `byte` | `getHour()` Get the hour component, 0-23
    `int` | `getMicrosecond()` Get the microsecond component, 0-999999
    `byte` | `getMinute()` Get the minute component, 0-59
    `byte` | `getMonth()` Get the month component, 1-12
    `CharSequence` | `getPrimitiveStringValue()` Convert to string
    `BuiltInAtomicType` | `getPrimitiveType()` Determine the primitive type of the value.
    `Comparable` | `getSchemaComparable()` Get a Comparable value that implements the XML Schema ordering comparison semantics for this value.
    `byte` | `getSecond()` Get the second component, 0-59
    `int` | `getYear()` Get the year component, in its internal form (which allows a year zero)
    `int` | `hashCode()` Hash code for context-free comparison of date time values.
    `boolean` | `isXsd10Rules()` Ask whether this value uses the XSD 1.0 rules (which don't allow year zero) or the XSD 1.1 rules (which do).
    `static ConversionResult` | `makeDateTimeValue(CharSequence s, ConversionRules rules)` Factory method: create a dateTime value from a supplied string, in ISO 8601 format
    `static DateTimeValue` | `makeDateTimeValue(DateValue date, TimeValue time)` Factory method: create a dateTime value given a date and a time.
    `DateTimeValue` | `normalize(XPathContext cc)` Normalize the date and time to be in timezone Z.
    `DayTimeDurationValue` | `subtract(CalendarValue other, XPathContext context)` Determine the difference between two points in time, as a duration
    `DateTimeValue` | `toDateTime()` Convert the value to a DateTime, retaining all the components that are actually present, and substituting conventional values for components that are missing.
    `DateValue` | `toDateValue()` Extract the Date part
    `BigDecimal` | `toJulianInstant()` Get the Julian instant: a decimal value whose integer part is the Julian day number multiplied by the number of seconds per day, and whose fractional part is the fraction of the second.
    `TimeValue` | `toTimeValue()` Extract the Time part

    - ### Methods inherited from class net.sf.saxon.value.CalendarValue

      `adjustTimezone, appendTimezone, appendTimezone, getTimezoneInMinutes, getXPathComparable, hasTimezone, isIdentical, makeCalendarValue, removeTimezone, setTimezoneInMinutes`

    - ### Methods inherited from class net.sf.saxon.value.AtomicValue

      `asAtomic, checkPermittedContents, effectiveBooleanValue, getCardinality, getItemType, getStringValue, getStringValueCS, head, isIdentical, isNaN, itemAt, iterate, process, setTypeLabel, toString`

    - ### Methods inherited from class net.sf.saxon.om.AbstractItem

      `getLength, reduce, subsequence`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface net.sf.saxon.om.AtomicSequence

      `getLength`

    - ### Methods inherited from interface net.sf.saxon.om.GroundedValue

      `reduce, subsequence`

- - ### Field Detail

    - #### EPOCH

          public static final DateTimeValue EPOCH

      Fixed date/time used by Java (and Unix) as the origin of the universe: 1970-01-01

  - ### Constructor Detail

    - #### DateTimeValue

          public DateTimeValue(Calendar calendar,
                       boolean tzSpecified)

      Constructor: create a dateTime value given a Java calendar object
      Parameters:
        `calendar` - holds the date and time
        `tzSpecified` - indicates whether the timezone is specified

    - #### DateTimeValue

          public DateTimeValue(int year,
                       byte month,
                       byte day,
                       byte hour,
                       byte minute,
                       byte second,
                       int microsecond,
                       int tz,
                       boolean xsd10Check)

      Constructor: construct a DateTimeValue from its components. This constructor performs no validation.
      Parameters:
        `year` - The year as held internally (note that the year before 1AD is 0)
        `month` - The month, 1-12
        `day` - The day 1-31
        `hour` - the hour value, 0-23
        `minute` - the minutes value, 0-59
        `second` - the seconds value, 0-59
        `microsecond` - the number of microseconds, 0-999999
        `tz` - the timezone displacement in minutes from UTC. Supply the value `CalendarValue.NO_TIMEZONE` if there is no timezone component.
        `xsd10Check` - true if the dateTime value should behave under XSD 1.0 rules, that is, negative dates assume there is no year zero. (Not that regardless of this setting, the year argument is set on the basis that the year before +1 is supplied as zero; but if the xsd10Check flag is set, this value will be displayed with a year of -1.)

  - ### Method Detail

    - #### getCurrentDateTime

          public static DateTimeValue getCurrentDateTime(XPathContext context)

      Get the dateTime value representing the nominal date/time of this transformation run. Two calls within the same query or transformation will always return the same answer.
      Parameters:
        `context` - the XPath dynamic context. May be null, in which case the current date and time are taken directly from the system clock
      Returns:
        the current xs:dateTime

    - #### fromJavaDate

          public static DateTimeValue fromJavaDate(Date suppliedDate)
                                            throws XPathException

      Factory method: create a dateTime value given a Java Date object. The returned dateTime value will always have a timezone, which will always be UTC.
      Parameters:
        `suppliedDate` - holds the date and time
      Returns:
        the corresponding xs:dateTime value
      Throws:
        `XPathException`

    - #### makeDateTimeValue

          public static DateTimeValue makeDateTimeValue(DateValue date,
                                        TimeValue time)
                                                 throws XPathException

      Factory method: create a dateTime value given a date and a time.
      Parameters:
        `date` - the date
        `time` - the time
      Returns:
        the dateTime with the given components. If either component is null, returns null
      Throws:
        `XPathException` - if the timezones are both present and inconsistent

    - #### makeDateTimeValue

          public static ConversionResult makeDateTimeValue(CharSequence s,
                                           ConversionRules rules)

      Factory method: create a dateTime value from a supplied string, in ISO 8601 format
      Parameters:
        `s` - a string in the lexical space of xs:dateTime
        `rules` - the conversion rules to be used (determining whether year zero is allowed)
      Returns:
        either a DateTimeValue representing the xs:dateTime supplied, or a ValidationFailure if the lexical value was invalid

    - #### convertToSubType

          public ValidationFailure convertToSubType(BuiltInAtomicType subtype)

      Convert the value to a built-in subtype of xs:dateTime
      Parameters:
        `subtype` - the target subtype
      Returns:
        null if the conversion succeeds; a ValidationFailure describing the failure if it fails.

    - #### getPrimitiveType

          public BuiltInAtomicType getPrimitiveType()

      Determine the primitive type of the value. This delivers the same answer as getItemType().getPrimitiveItemType(). The primitive types are the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration, and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
      Specified by:
        `getPrimitiveType` in class `AtomicValue`
      Returns:
        the primitive type

    - #### getYear

          public int getYear()

      Get the year component, in its internal form (which allows a year zero)
      Returns:
        the year component

    - #### getMonth

          public byte getMonth()

      Get the month component, 1-12
      Returns:
        the month component

    - #### getDay

          public byte getDay()

      Get the day component, 1-31
      Returns:
        the day component

    - #### getHour

          public byte getHour()

      Get the hour component, 0-23
      Returns:
        the hour component (never 24, even if the input was specified as 24:00:00)

    - #### getMinute

          public byte getMinute()

      Get the minute component, 0-59
      Returns:
        the minute component

    - #### getSecond

          public byte getSecond()

      Get the second component, 0-59
      Returns:
        the second component

    - #### getMicrosecond

          public int getMicrosecond()

      Get the microsecond component, 0-999999
      Returns:
        the microsecond component

    - #### toDateTime

          public DateTimeValue toDateTime()

      Convert the value to a DateTime, retaining all the components that are actually present, and substituting conventional values for components that are missing. (This method does nothing in the case of xs:dateTime, but is there to implement a method in the `CalendarValue` interface).
      Specified by:
        `toDateTime` in class `CalendarValue`
      Returns:
        the value as an xs:dateTime

    - #### isXsd10Rules

          public boolean isXsd10Rules()

      Ask whether this value uses the XSD 1.0 rules (which don't allow year zero) or the XSD 1.1 rules (which do).
      Returns:
        true if the value uses the XSD 1.0 rules

    - #### normalize

          public DateTimeValue normalize(XPathContext cc)
                                  throws NoDynamicContextException

      Normalize the date and time to be in timezone Z.
      Parameters:
        `cc` - used to supply the implicit timezone, used when the value has no explicit timezone
      Returns:
        in general, a new DateTimeValue in timezone Z, representing the same instant in time. Returns the original DateTimeValue if this is already in timezone Z.
      Throws:
        `NoDynamicContextException` - if the implicit timezone is needed and is not available

    - #### getComparisonKey

          public ComparisonKey getComparisonKey(XPathContext context)
                                         throws NoDynamicContextException

      Get a comparison key for this value. Two values are equal if and only if they their comparison keys are equal
      Specified by:
        `getComparisonKey` in class `CalendarValue`
      Parameters:
        `context` - XPath dynamic context
      Returns:
        a comparison key
      Throws:
        `NoDynamicContextException` - if the implicit timezone is needed and is not available

    - #### toJulianInstant

          public BigDecimal toJulianInstant()

      Get the Julian instant: a decimal value whose integer part is the Julian day number multiplied by the number of seconds per day, and whose fractional part is the fraction of the second. This method operates on the local time, ignoring the timezone. The caller should call normalize() before calling this method to get a normalized time.
      Returns:
        the Julian instant corresponding to this xs:dateTime value

    - #### fromJulianInstant

          public static DateTimeValue fromJulianInstant(BigDecimal instant)

      Get the DateTimeValue corresponding to a given Julian instant
      Parameters:
        `instant` - the Julian instant: a decimal value whose integer part is the Julian day number multiplied by the number of seconds per day, and whose fractional part is the fraction of the second.
      Returns:
        the xs:dateTime value corresponding to the Julian instant. This will always be in timezone Z.

    - #### getCalendar

          public GregorianCalendar getCalendar()

      Get a Java Calendar object representing the value of this DateTime. This will respect the timezone if there is one, or be in GMT otherwise.
      Specified by:
        `getCalendar` in class `CalendarValue`
      Returns:
        a Java GregorianCalendar object representing the value of this xs:dateTime value.

    - #### getPrimitiveStringValue

          public CharSequence getPrimitiveStringValue()

      Convert to string
      Specified by:
        `getPrimitiveStringValue` in class `AtomicValue`
      Returns:
        ISO 8601 representation. The value returned is the localized representation, that is it uses the timezone contained within the value itself.

    - #### toDateValue

          public DateValue toDateValue()

      Extract the Date part
      Returns:
        a DateValue representing the date part of the dateTime, retaining the timezone or its absence

    - #### toTimeValue

          public TimeValue toTimeValue()

      Extract the Time part
      Returns:
        a TimeValue representing the date part of the dateTime, retaining the timezone or its absence

    - #### getCanonicalLexicalRepresentation

          public CharSequence getCanonicalLexicalRepresentation()

      Get the canonical lexical representation as defined in XML Schema. This is not always the same as the result of casting to a string according to the XPath rules. For an xs:dateTime it is the date/time adjusted to UTC.
      Specified by:
        `getCanonicalLexicalRepresentation` in interface `AtomicSequence`
      Overrides:
        `getCanonicalLexicalRepresentation` in class `AtomicValue`
      Returns:
        the canonical lexical representation as defined in XML Schema

    - #### copyAsSubType

          public AtomicValue copyAsSubType(AtomicType typeLabel)

      Make a copy of this date, time, or dateTime value, but with a new type label
      Specified by:
        `copyAsSubType` in class `AtomicValue`
      Parameters:
        `typeLabel` - the type label to be attached to the new copy. It is the caller's responsibility to ensure that the value actually conforms to the rules for this type.
      Returns:
        the copied value

    - #### adjustTimezone

          public CalendarValue adjustTimezone(int timezone)

      Return a new dateTime with the same normalized value, but in a different timezone.
      Specified by:
        `adjustTimezone` in class `CalendarValue`
      Parameters:
        `timezone` - the new timezone offset, in minutes
      Returns:
        the date/time in the new timezone. This will be a new DateTimeValue unless no change was required to the original value

    - #### add

          public CalendarValue add(DurationValue duration)
                            throws XPathException

      Add a duration to a dateTime
      Specified by:
        `add` in class `CalendarValue`
      Parameters:
        `duration` - the duration to be added (may be negative)
      Returns:
        the new date
      Throws:
        `XPathException` - if the duration is an xs:duration, as distinct from a subclass thereof

    - #### subtract

          public DayTimeDurationValue subtract(CalendarValue other,
                                      XPathContext context)
                                        throws XPathException

      Determine the difference between two points in time, as a duration
      Overrides:
        `subtract` in class `CalendarValue`
      Parameters:
        `other` - the other point in time
        `context` - the XPath dynamic context
      Returns:
        the duration as an xs:dayTimeDuration
      Throws:
        `XPathException` - for example if one value is a date and the other is a time

    - #### getComponent

          public AtomicValue getComponent(int component)
                                   throws XPathException

      Get a component of the value. Returns null if the timezone component is requested and is not present.
      Overrides:
        `getComponent` in class `AtomicValue`
      Parameters:
        `component` - identifies the required component, as a constant defined in class `Component`, for example `Component.HOURS`
      Returns:
        the value of the requested component of this value
      Throws:
        `XPathException` - if a dynamic error occurs

    - #### compareTo

          public int compareTo(CalendarValue other,
                      XPathContext context)
                        throws NoDynamicContextException

      Compare the value to another dateTime value, following the XPath comparison semantics
      Specified by:
        `compareTo` in class `CalendarValue`
      Parameters:
        `other` - The other dateTime value
        `context` - XPath dynamic evaluation context
      Returns:
        negative value if this one is the earler, 0 if they are chronologically equal, positive value if this one is the later. For this purpose, dateTime values with an unknown timezone are considered to be values in the implicit timezone (the Comparable interface requires a total ordering).
      Throws:
        `ClassCastException` - if the other value is not a DateTimeValue (the parameter is declared as CalendarValue to satisfy the interface)
        `NoDynamicContextException` - if the implicit timezone is needed and is not available

    - #### compareTo

          public int compareTo(Object v2)

      Context-free comparison of two DateTimeValue values. For this to work, the two values must either both have a timezone or both have none.
      Specified by:
        `compareTo` in interface `Comparable`
      Parameters:
        `v2` - the other value
      Returns:
        the result of the comparison: -1 if the first is earlier, 0 if they are equal, +1 if the first is later
      Throws:
        `ClassCastException` - if the values are not comparable (which might be because no timezone is available)

    - #### getSchemaComparable

          public Comparable getSchemaComparable()

      Description copied from class: `AtomicValue`
      Get a Comparable value that implements the XML Schema ordering comparison semantics for this value. An implementation must be provided for all atomic types.

      In the case of data types that are partially ordered, the returned Comparable extends the standard semantics of the compareTo() method by returning the value `SequenceTool.INDETERMINATE_ORDERING` when there is no defined order relationship between two given values. This value is also returned when two values of different types are compared.

      Specified by:
        `getSchemaComparable` in interface `AtomicSequence`
      Specified by:
        `getSchemaComparable` in class `AtomicValue`
      Returns:
        a Comparable that follows XML Schema comparison rules

    - #### equals

          public boolean equals(Object o)

      Context-free comparison of two dateTime values
      Specified by:
        `equals` in class `AtomicValue`
      Parameters:
        `o` - the other date time value
      Returns:
        true if the two values represent the same instant in time
      Throws:
        `ClassCastException` - if one of the values has a timezone and the other does not

    - #### hashCode

          public int hashCode()

      Hash code for context-free comparison of date time values. Note that equality testing and therefore hashCode() works only for values with a timezone
      Overrides:
        `hashCode` in class `Object`
      Returns:
        a hash code

Copyright (c) 2004-2013 Saxonica Limited. All rights reserved.