Class DateValue

All Implemented Interfaces:
Comparable<XPathComparable>, Iterable<AtomicValue>, AtomicMatchKey, XPathComparable, AtomicSequence, GroundedValue, IdentityComparable, Item, Sequence, ConversionResult

public class DateValue extends GDateValue implements XPathComparable
A value of type xs:date. Note that a Date may include a TimeZone.
  • Constructor Details

    • DateValue

      public DateValue(int year, int month, int day)
      Constructor given a year, month, and day. Performs no validation.
      Parameters:
      year - The year. Accepts year zero.
      month - The month, 1-12
      day - The day, 1-31
    • DateValue

      public DateValue(int year, int month, int day, int tz)
      Constructor given a year, month, and day, and timezone. Performs no validation.
      Parameters:
      year - The year. Accepts year zero
      month - The month, 1-12
      day - The day, 1-31
      tz - the timezone displacement in minutes from UTC. Supply the value CalendarValue.NO_TIMEZONE if there is no timezone component.
    • DateValue

      public DateValue(int year, int month, int day, int tz, AtomicMetadata type)
      Constructor given a year, month, and day, and timezone, and an AtomicType representing a subtype of xs:date. Performs no validation.
      Parameters:
      year - The year. Accepts year zero.
      month - The month, 1-12
      day - The day 1-31
      tz - the timezone displacement in minutes from UTC. Supply the value CalendarValue.NO_TIMEZONE if there is no timezone component.
      type - the type. This must be a type derived from xs:date, and the value must conform to this type. The method does not check these conditions.
    • DateValue

      public DateValue(LocalDate localDate)
      Create a DateValue (with no timezone) from a Java LocalDate object
      Parameters:
      localDate - the supplied local date
      Since:
      10.0
    • DateValue

      public DateValue(GregorianCalendar calendar, int tz)
      Create a DateValue from a Java GregorianCalendar object
      Parameters:
      calendar - the absolute date/time value
      tz - The timezone offset from GMT in minutes, positive or negative; or the special value NO_TIMEZONE indicating that the value is not in a timezone
  • Method Details

    • tryParseDate

      public static ConversionResult tryParseDate(String s, boolean allowYearZero)
      Initialize the DateValue using a character string in the format yyyy-mm-dd and an optional time zone. Input must have format [-]yyyy-mm-dd[([+|-]hh:mm | Z)]
      Parameters:
      s - the supplied string value
      allowYearZero - true if (as in XSD 1.1) there is a year zero, false if (as in XSD 1.0) there is not
      Returns:
      either a DateValue if the string represents a valid date, or a ValidationFailure otherwise.
    • parseDate

      public static DateValue parseDate(String value) throws ValidationException
      Construct a DateValue, from its lexical representation
      Parameters:
      value - the lexical representation of the date
      Returns:
      the corresponding date
      Throws:
      ValidationException - if the date is invalid
    • makeDateValue

      public static ConversionResult makeDateValue(int year, int month, int day, int timezoneInMinutes)
      Creates an instance of DateValue. Includes validation checks. If a validation error is detected, an instance of ValidationFailure will be returned instead.
      Parameters:
      year - - number of a year in the Gregorian calendar
      month - - number of a month within the specified year
      day - - number of a day within the specified month
      timezoneInMinutes - - number of minutes to adjust by for the timezone
      Returns:
      an instance of GYearMonthValue or ValidationFailure
    • 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
    • tomorrow

      public static DateValue tomorrow(int year, int month, int day)
      Get the date that immediately follows a given date
      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      a new DateValue with no timezone information
    • yesterday

      public static DateValue yesterday(int year, int month, int day)
      Get the date that immediately precedes a given date
      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      a new DateValue with no timezone information
    • getPrimitiveStringValue

      public UnicodeString getPrimitiveStringValue()
      Convert to string
      Specified by:
      getPrimitiveStringValue in class AtomicValue
      Returns:
      ISO 8601 representation.
    • getCanonicalLexicalRepresentation

      public UnicodeString 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 xs:date, the timezone is adjusted to be in the range +12:00 to -11:59
      Specified by:
      getCanonicalLexicalRepresentation in interface AtomicSequence
      Overrides:
      getCanonicalLexicalRepresentation in class AtomicValue
      Returns:
      the canonical lexical representation if defined in XML Schema; otherwise, the result of casting to string according to the XPath 2.0 rules
    • withMetadata

      public AtomicValue withMetadata(AtomicMetadata metadata)
      Make a copy of this date value, but with a new type label
      Specified by:
      withMetadata in class AtomicValue
      Parameters:
      metadata - the new type label: must be a subtype of xs:date
      Returns:
      the new xs:date value
    • adjustTimezone

      public DateValue adjustTimezone(int timezone)
      Return a new date with the same normalized value, but in a different timezone. This is called only for a DateValue that has an explicit timezone
      Specified by:
      adjustTimezone in class CalendarValue
      Parameters:
      timezone - the new timezone offset, in minutes
      Returns:
      the time in the new timezone. This will be a new TimeValue unless no change was required to the original value
    • add

      public DateValue add(DurationValue duration) throws XPathException
      Add a duration to a date
      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. May be set to null only if both values contain an explicit timezone, or if neither does so.
      Returns:
      the duration as an xs:dayTimeDuration
      Throws:
      XPathException - for example if one value is a date and the other is a time
    • getXPathComparable

      public XPathComparable getXPathComparable(StringCollator collator, int implicitTimezone, int specVersion) throws NoDynamicContextException
      Description copied from class: AtomicValue
      Get an object value that implements the XPath equality and ordering comparison semantics for this value. A collation is supplied for comparing strings, and an implicit timezone for comparing date/time values that have no saved timezone. An atomic value may return itself as the result, provided that its ordering rules are independent of the collation and timezone, and provided that it implements the XPathComparable interface: which means that its compareTo, equals, and hashCode methods must be compatible with the rules for XPath value comparisons.
      Overrides:
      getXPathComparable in class GDateValue
      Parameters:
      collator - the collation to be used when comparing strings
      implicitTimezone - the implicit timezone in the dynamic context, used when comparing dates/times with and without timezone
      specVersion - the version of the XPath specification (31 for 3.1, 40 for 4.0)
      Returns:
      an Object that implements the XPath value comparison semantics with respect to this atomic value. For an atomic type that is not ordered (according to XPath rules), return null.
      Throws:
      NoDynamicContextException - if the supplied implicit timezone is "NO_TIMEZONE" (meaning unknown), and the implicit timezone is actually required because the value in question is a date/time value with no timezone. This can cause a failure to evaluate expressions statically (because the implicit timezone is not known statically), and it will then be caught, meaning that the expression has to be evaluated dynamically.
    • compareTo

      public int compareTo(XPathComparable v2)
      Context-free comparison of two DateValue values. For this to work, the two values must either both have a timezone or both have none.
      Specified by:
      compareTo in interface Comparable<XPathComparable>
      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)
    • getJulianDayNumber

      public static int getJulianDayNumber(int year, int month, int day)
      Calculate the Julian day number at 00:00 on a given date. This algorithm is taken from http://vsg.cape.com/~pbaum/date/jdalg.htm and http://vsg.cape.com/~pbaum/date/jdalg2.htm (adjusted to handle BC dates correctly)

      Note that this assumes dates in the proleptic Gregorian calendar

      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      the Julian day number
    • getJulianDayNumber

      public int getJulianDayNumber()
      Calculate the Julian day number at 00:00 on this date.

      Note that this assumes dates in the proleptic Gregorian calendar

      Returns:
      the Julian day number
    • dateFromJulianDayNumber

      public static DateValue dateFromJulianDayNumber(long julianDayNumber)
      Get the Gregorian date corresponding to a particular Julian day number. The algorithm is taken from http://www.hermetic.ch/cal_stud/jdn.htm#comp
      Parameters:
      julianDayNumber - the Julian day number
      Returns:
      a DateValue with no timezone information set
    • getDayWithinYear

      public static int getDayWithinYear(int year, int month, int day)
      Get the ordinal day number within the year (1 Jan = 1, 1 Feb = 32, etc)
      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      the ordinal day number within the year
    • getDayOfWeek

      public static int getDayOfWeek(int year, int month, int day)
      Get the day of the week. The days of the week are numbered from 1 (Monday) to 7 (Sunday)
      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      the day of the week, 1=Monday .... 7=Sunday
    • getWeekNumber

      public static int getWeekNumber(int year, int month, int day)
      Get the ISO week number for a given date. The days of the week are numbered from 1 (Monday) to 7 (Sunday), and week 1 in any calendar year is the week (from Monday to Sunday) that includes the first Thursday of that year
      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      the ISO week number
    • getWeekNumberWithinMonth

      public static int getWeekNumberWithinMonth(int year, int month, int day)
      Get the week number within a month. This is required for the XSLT format-date() function. The days of the week are numbered from 1 (Monday) to 7 (Sunday), and week 1 in any calendar month is the week (from Monday to Sunday) that includes the first Thursday of that month.

      See bug 21370 which clarified the specification. This caused a change to the Saxon implementation such that the days before the start of week 1 go in the last week of the previous month, not week zero.

      Parameters:
      year - the year
      month - the month (1-12)
      day - the day (1-31)
      Returns:
      the week number within a month
    • toLocalDate

      public LocalDate toLocalDate()
      Convert the value to a Java LocalDate value, dropping any timezone information
      Returns:
      the corresponding Java LocalDate; any timezone information is simply discarded