public final class DateTimeValue extends CalendarValue implements java.lang.Comparable, java.time.temporal.TemporalAccessor
xs:dateTime
. This contains integer fields year, month, day, hour, minute,
second, and nanosecond. All these fields except year must be non-negative. In the internal
representation, the sequence of years runs -2, -1, 0, +1, +2: that is, the year before year 1
is year 0.
The value also contains a boolean flag hasNoYearZero
. When this flag is set, accessor
methods that expose the year value subtract one if it is non-positive: that is, year 0 is displayed
as -1, year -1 as -2, and so on. Constructor methods, unless otherwise specified, do not set this
flag.
From Saxon 9.9, this class implements the Java 8 interface TemporalAccessor
which enables
it to interoperate with Java 8 temporal classes such as Instant
and ZonedDateTime
.
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-01T00:00:00Z
|
MISSING_TIMEZONE, NO_TIMEZONE
typeLabel
NaN_MATCH_KEY
Constructor and Description |
---|
DateTimeValue(java.util.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 nanosecond,
int tz)
Constructor: construct a DateTimeValue from its components.
|
DateTimeValue(int year,
byte month,
byte day,
byte hour,
byte minute,
byte second,
int microsecond,
int tz,
boolean hasNoYearZero)
Constructor: construct a DateTimeValue from its components.
|
Modifier and Type | Method and Description |
---|---|
DateTimeValue |
add(DurationValue duration)
Add a duration to a dateTime
|
DateTimeValue |
adjustTimezone(int timezone)
Return a new dateTime with the same normalized value, but
in a different timezone.
|
DateTimeValue |
adjustToUTC(int implicitTimezone)
Normalize the date and time to be in timezone Z.
|
void |
checkValidInJavascript()
Check that the value can be handled in Saxon-JS
|
int |
compareTo(CalendarValue other,
int implicitTimezone)
Compare the value to another dateTime value, following the XPath comparison semantics
|
int |
compareTo(java.lang.Object v2)
Context-free comparison of two DateTimeValue values.
|
DateTimeValue |
copyAsSubType(AtomicType typeLabel)
Make a copy of this date, time, or dateTime value, but with a new type label
|
boolean |
equals(java.lang.Object o)
Context-free comparison of two dateTime values
|
static DateTimeValue |
fromJavaDate(java.util.Date suppliedDate)
Factory method: create a dateTime value given a Java Date object.
|
static DateTimeValue |
fromJavaInstant(java.time.Instant instant)
Factory method: create a dateTime value given a Java
Instant . |
static DateTimeValue |
fromJavaInstant(long seconds,
int nano)
Factory method: create a dateTime value given the components of a Java Instant.
|
static DateTimeValue |
fromJavaTime(long time)
Factory method: create a dateTime value given a Java time, expressed in milliseconds since 1970.
|
static DateTimeValue |
fromJulianInstant(java.math.BigDecimal instant)
Get the DateTimeValue corresponding to a given Julian instant
|
static DateTimeValue |
fromLocalDateTime(java.time.LocalDateTime localDateTime)
Factory method: create a dateTime value given a Java
LocalDateTime . |
static DateTimeValue |
fromZonedDateTime(java.time.ZonedDateTime zonedDateTime)
Factory method: create a dateTime value given a Java
ZonedDateTime . |
java.util.GregorianCalendar |
getCalendar()
Get a Java Calendar object representing the value of this DateTime.
|
java.lang.CharSequence |
getCanonicalLexicalRepresentation()
Get the canonical lexical representation as defined in XML Schema.
|
AtomicValue |
getComponent(AccessorFn.Component 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
|
long |
getLong(java.time.temporal.TemporalField field)
Gets the value of the specified field as a
long . |
int |
getMicrosecond()
Get the microsecond component, 0-999999
|
byte |
getMinute()
Get the minute component, 0-59
|
byte |
getMonth()
Get the month component, 1-12
|
int |
getNanosecond()
Get the nanosecond component, 0-999999
|
java.lang.CharSequence |
getPrimitiveStringValue()
Convert to string
|
BuiltInAtomicType |
getPrimitiveType()
Determine the primitive type of the value.
|
java.lang.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 |
isSupported(java.time.temporal.TemporalField field) |
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(java.lang.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.
|
static DateTimeValue |
now()
Get the dateTime value representing the moment of invocation of this method,
in the default timezone set for the platform on which the application is running.
|
static DateTimeValue |
parse(java.lang.CharSequence s)
Factory method: create a dateTime value from a supplied string, in ISO 8601 format, allowing
a year value of 0 to represent the year before year 1 (that is, following the XSD 1.1 rules).
|
java.math.BigDecimal |
secondsSinceEpoch() |
DayTimeDurationValue |
subtract(CalendarValue other,
XPathContext context)
Determine the difference between two points in time, as a duration
|
DateTimeValue |
toDateTime()
Convert the value to an xs:dateTime, retaining all the components that are actually present, and
substituting conventional values for components that are missing.
|
DateValue |
toDateValue()
Extract the Date part
|
java.time.Instant |
toJavaInstant()
Get a Java 8
Instant corresponding to this date and time. |
java.math.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.
|
java.time.LocalDateTime |
toLocalDateTime()
Get a Java 8
LocalDateTime corresponding to this date and time. |
TimeValue |
toTimeValue()
Extract the Time part
|
java.time.ZonedDateTime |
toZonedDateTime()
Get a Java 8
ZonedDateTime corresponding to this date and time. |
adjustTimezone, appendTimezone, appendTimezone, asMapKey, getComparisonKey, getTimezoneInMinutes, getXMLGregorianCalendar, getXPathComparable, hasTimezone, identityHashCode, isIdentical, makeCalendarValue, removeTimezone, setTimezoneInMinutes
asAtomic, atomize, checkPermittedContents, effectiveBooleanValue, getCardinality, getGenre, getItemType, getLength, getStringValue, getStringValueCS, getUType, head, isIdentical, isNaN, itemAt, iterate, iterator, process, setTypeLabel, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
asAtomic
isStreamed, reduce, subsequence, toGroundedValue, toShortString
asIterable, materialize
makeRepeatable
public static final DateTimeValue EPOCH
public DateTimeValue(java.util.Calendar calendar, boolean tzSpecified)
#hasNoYearZero
flag is set to true
.calendar
- holds the date and timetzSpecified
- indicates whether the timezone is specifiedpublic DateTimeValue(int year, byte month, byte day, byte hour, byte minute, byte second, int nanosecond, int tz)
Note: this constructor accepts the fractional seconds value
to nanosecond precision. It creates a DateTimeValue that follows XSD 1.1 conventions:
that is, there is a year zero. This can be changed by setting the hasNoYearZero
property.
year
- The year (the year before year 1 is year 0)month
- The month, 1-12day
- The day 1-31hour
- the hour value, 0-23minute
- the minutes value, 0-59second
- the seconds value, 0-59nanosecond
- the number of nanoseconds, 0-999_999_999tz
- the timezone displacement in minutes from UTC. Supply the value
CalendarValue.NO_TIMEZONE
if there is no timezone component.public DateTimeValue(int year, byte month, byte day, byte hour, byte minute, byte second, int microsecond, int tz, boolean hasNoYearZero)
Note: for historic reasons, this constructor accepts the fractional seconds value only to microsecond precision. To get nanosecond precision, use the 8-argument constructor and set the XSD 1.0 option separately
year
- The year as held internally (so the year before year 1 is year 0)month
- The month, 1-12day
- The day 1-31hour
- the hour value, 0-23minute
- the minutes value, 0-59second
- the seconds value, 0-59microsecond
- the number of microseconds, 0-999999tz
- the timezone displacement in minutes from UTC. Supply the value
CalendarValue.NO_TIMEZONE
if there is no timezone component.hasNoYearZero
- true if the dateTime value should behave under XSD 1.0 rules, that is,
negative dates assume there is no year zero. Note 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 hasNoYearZero flag is set, this value will be displayed
with a year of -1, and will return -1
public static DateTimeValue getCurrentDateTime(XPathContext context)
context
- the XPath dynamic context. May be null, in which case
the current date and time are taken directly from the system clockpublic static DateTimeValue now()
public static DateTimeValue fromJavaDate(java.util.Date suppliedDate) throws XPathException
suppliedDate
- holds the date and timeXPathException
- if a dynamic error occurspublic static DateTimeValue fromJavaTime(long time) throws XPathException
time
- the time in milliseconds since the epochXPathException
- if a dynamic error occurspublic static DateTimeValue fromJavaInstant(long seconds, int nano) throws XPathException
seconds
- the time in seconds since the epochnano
- the additional nanosecondsXPathException
- if a dynamic error occurs (typically due to overflow)public static DateTimeValue fromJavaInstant(java.time.Instant instant)
Instant
. The java.time.Instant
class
is new in JDK 8. Note that DateTimeValue
only holds microsecond precision, so there will be some
rounding.instant
- the point in timepublic static DateTimeValue fromZonedDateTime(java.time.ZonedDateTime zonedDateTime)
ZonedDateTime
. The java.time.ZonedDateTime
class
is new in JDK 8. Note that DateTimeValue
only holds microsecond precision, so there will be some
rounding.zonedDateTime
- the supplied zonedDateTime valuexs:dateTimeStamp
public static DateTimeValue fromLocalDateTime(java.time.LocalDateTime localDateTime)
LocalDateTime
. The java.time.LocalDateTime
class
is new in JDK 8. Note that DateTimeValue
only holds microsecond precision, so there will be some
rounding.localDateTime
- the supplied localDateTime valuepublic static DateTimeValue makeDateTimeValue(DateValue date, TimeValue time) throws XPathException
date
- the datetime
- the timeDateTimeValue
will have the #hasNoYearZero
property if and only if the supplied
date has this property.XPathException
- if the timezones are both present and inconsistentpublic static ConversionResult makeDateTimeValue(java.lang.CharSequence s, ConversionRules rules)
If the supplied ConversionRules
object has ConversionRules.isAllowYearZero()
returning
true, then (a) a year value of zero is allowed in the supplied string, and (b) the hasNoYearZero
property in the result is set to false. If ConversionRules.isAllowYearZero()
returns false,
the (a) the year value in the supplied string must not be zero, (b) a year value of -1 in the supplied
string is interpreted as representing the year before year 1, and (c) the hasNoYearZero
property
in the result is set to true.
s
- a string in the lexical space of xs:dateTimerules
- the conversion rules to be used (determining whether year zero is allowed)public static DateTimeValue parse(java.lang.CharSequence s) throws java.time.format.DateTimeParseException
The hasNoYearZero
property in the result is set to false.
s
- a string in the lexical space of xs:dateTimejava.time.format.DateTimeParseException
- if the format of the supplied string is invalid.public BuiltInAtomicType getPrimitiveType()
getPrimitiveType
in class AtomicValue
public int getYear()
public byte getMonth()
public byte getDay()
public byte getHour()
public byte getMinute()
public byte getSecond()
public int getMicrosecond()
public int getNanosecond()
public DateTimeValue toDateTime()
CalendarValue
interface).toDateTime
in class CalendarValue
public boolean isXsd10Rules()
public void checkValidInJavascript() throws XPathException
checkValidInJavascript
in class AtomicValue
XPathException
- if it can't be handled in Saxon-JSpublic DateTimeValue adjustToUTC(int implicitTimezone) throws NoDynamicContextException
implicitTimezone
- used to supply the implicit timezone, used when the value has
no explicit timezoneNoDynamicContextException
- if the implicit timezone is needed and is CalendarValue.MISSING_TIMEZONE
or CalendarValue.NO_TIMEZONEpublic java.math.BigDecimal toJulianInstant()
public static DateTimeValue fromJulianInstant(java.math.BigDecimal instant)
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.public java.util.GregorianCalendar getCalendar()
GregorianCalendar
class, which in practice means that it is not -14:00). If there is no timezone or if
the timezone is out of range, the result will be in GMT.getCalendar
in class CalendarValue
public java.time.Instant toJavaInstant()
Instant
corresponding to this date and time. The value will respect the time zone
offset if present, or will assume UTC otherwise.public java.time.ZonedDateTime toZonedDateTime()
ZonedDateTime
corresponding to this date and time. The value will respect the time zone
offset if present, or will assume UTC otherwise.ZonedDateTime
representing this date and time, including its timezone if present, or
interpreted as a UTC date/time otherwise.public java.time.LocalDateTime toLocalDateTime()
LocalDateTime
corresponding to this date and time. The value will ignore any timezone
offset present in this value.LocalDateTime
equivalent to this date and time, discarding any time zone offset if present.public java.lang.CharSequence getPrimitiveStringValue()
getPrimitiveStringValue
in class AtomicValue
hasNoYearZero
flag is set, in which case it is the
internal year minus one.public DateValue toDateValue()
public TimeValue toTimeValue()
public java.lang.CharSequence getCanonicalLexicalRepresentation()
getCanonicalLexicalRepresentation
in interface AtomicSequence
getCanonicalLexicalRepresentation
in class AtomicValue
public DateTimeValue copyAsSubType(AtomicType typeLabel)
copyAsSubType
in class AtomicValue
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.public DateTimeValue adjustTimezone(int timezone)
adjustTimezone
in class CalendarValue
timezone
- the new timezone offset, in minutespublic DateTimeValue add(DurationValue duration) throws XPathException
add
in class CalendarValue
duration
- the duration to be added (may be negative)XPathException
- if the duration is an xs:duration, as distinct from
a subclass thereofpublic DayTimeDurationValue subtract(CalendarValue other, XPathContext context) throws XPathException
subtract
in class CalendarValue
other
- the other point in timecontext
- the XPath dynamic contextXPathException
- for example if one value is a date and the other is a timepublic java.math.BigDecimal secondsSinceEpoch()
public AtomicValue getComponent(AccessorFn.Component component) throws XPathException
getComponent
in class AtomicValue
component
- identifies the required componentXPathException
- if a dynamic error occurspublic boolean isSupported(java.time.temporal.TemporalField field)
isSupported
in interface java.time.temporal.TemporalAccessor
public long getLong(java.time.temporal.TemporalField field)
long
.
This queries the date-time for the value of the specified field. The returned value may be outside the valid range of values for the field. If the date-time cannot return the value, because the field is unsupported or for some other reason, an exception will be thrown.
The specification requires some fields (for example ChronoField.EPOCH_DAY
to
reflect the local time. The Saxon implementation does not have access to the local time,
so it adjusts to UTC instead.
getLong
in interface java.time.temporal.TemporalAccessor
field
- the field to get, not nulljava.time.DateTimeException
- if a value for the field cannot be obtainedjava.time.temporal.UnsupportedTemporalTypeException
- if the field is not supportedjava.lang.ArithmeticException
- if numeric overflow occurspublic int compareTo(CalendarValue other, int implicitTimezone) throws NoDynamicContextException
compareTo
in class CalendarValue
other
- The other dateTime valueimplicitTimezone
- The implicit timezone to be used for a value with no timezonejava.lang.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 availablepublic int compareTo(java.lang.Object v2)
compareTo
in interface java.lang.Comparable
v2
- the other valuejava.lang.ClassCastException
- if the values are not comparable (which might be because
no timezone is available)public java.lang.Comparable getSchemaComparable()
AtomicValue
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.
getSchemaComparable
in interface AtomicSequence
getSchemaComparable
in class AtomicValue
public boolean equals(java.lang.Object o)
equals
in class AtomicValue
o
- the other date time valuepublic int hashCode()
hashCode
in class java.lang.Object
Copyright (c) 2004-2020 Saxonica Limited. All rights reserved.