Class DateAndTime

  • All Implemented Interfaces:
    DataObject, FastSerializable, Serializable

    public final class DateAndTime
    extends AbstractLazyEncodedByteArray
    implements FastSerializable
    Class DateAndTime represents a date and time. It represents the ASN.1 OCTET STRING types Core-INAP-CS1-DataTypes.DateAndTime and CAP-DataTypes.DateAndTime.

    The API offered by this class is as if the type were specified in ASN.1 as follows.

     DateAndTime ::= SEQUENCE {
         year   INTEGER,
         month  INTEGER,
         day    INTEGER,
         hour   INTEGER,
         minute INTEGER,
         second INTEGER
     }
    The class takes care of encoding the field values into a byte-array and decoding field values from a byte-array, the encoded representation being as tabulated below. The digits are encoded separately in BCD. An INAP date has only two digits in the year, whereas a CAP date has four digits in the year.

    Data Encoding - based on ETSI TS 300 374-1 September 1994 §6.3 p46
    87654321
    Octet 0Year unitsYear tens
    Octet 1Month unitsMonth tens
    Octet 2Day unitsDay tens
    Octet 3Hours unitsHours tens
    Octet 4Minutes unitsMinutes tens
    Octet 5Seconds unitsSeconds tens

    For example, 1993 September 30th at 12:15:01 is encoded as 399003215110 (assuming the high nibble is written before the low nibble).

    Data Encoding - based on ETSI TS 101 046 v7.1.0 (2000-07) p40
    87654321
    Octet 0Year hundredsYear thousands
    Octet 1Year unitsYear tens
    Octet 2Month unitsMonth tens
    Octet 3Day unitsDay tens
    Octet 4Hours unitsHours tens
    Octet 5Minutes unitsMinutes tens
    Octet 6Seconds unitsSeconds tens

    For example, 1993 September 30th at 12:15:01 is encoded as 91399003215110 (assuming the high nibble is written before the low nibble).

    Note that the INAP form does not store millenium or century information. When encoding to INAP, the millenium and century digits are ignored. When decoding from INAP, a millenium and century are selected so that the resulting year lies in the range [1970,2069]; if a different range is needed, then use the returned year modulo 100 to obtain the raw value.

    See Also:
    Serialized Form
    • Constructor Detail

      • DateAndTime

        public DateAndTime()
        Constructs a new DateAndTime object with no fields set.
      • DateAndTime

        public DateAndTime​(byte[] data)
        Constructs a new DateAndTime object from network-encoded data. The data is not decoded and might not be decodable.
        Parameters:
        data - network-encoded data
        Throws:
        NullPointerException - if data is null
      • DateAndTime

        public DateAndTime​(byte[] data,
                           int start,
                           int len)
        Constructs a new DateAndTime object from part of network-encoded data. The part starts at index start and is len bytes long. The data is not decoded and might not be decodable.
        Parameters:
        data - network-encoded data
        start - starting offset of network-encoded data in byte array
        len - default length if not predictable
        Throws:
        NullPointerException - if data is null
        IllegalArgumentException - if len is negative
      • DateAndTime

        public DateAndTime​(int year,
                           int month,
                           int day,
                           int hour,
                           int minute,
                           int second)
        Constructs a new DateAndTime object from given values for all fields.
        Parameters:
        year - the year, in range [0000,9999]
        month - the month, in range [1,12]
        day - the day of the month, in range [1,31]
        hour - the hour, in range [0,23]
        minute - the minute, in range [0,59]
        second - the second, in range [0,59]
      • DateAndTime

        public DateAndTime​(DataInput in)
                    throws IOException
        Constructs a new DateAndTime object from data deserialized from a stream that was written by toStream(DataOutput).
        Parameters:
        in - the stream to read from
        Throws:
        EOFException - if reading is pre-empted by end-of-file
        IOException - if the data cannot be read
    • Method Detail

      • hasYear

        public boolean hasYear()
                        throws DecodeException
        Tests whether the field Year has a value.
        Returns:
        whether the field has a value
        Throws:
        DecodeException - if encoded state cannot be decoded
      • setYearPresent

        public DateAndTime setYearPresent​(boolean flag)
                                   throws IllegalStateException
        Sets the presence or absence of the field Year.
        Parameters:
        flag - whether the field should be marked as present
        Returns:
        this
        Throws:
        IllegalStateException - if this instance has been marked as read-only
      • hasMonth

        public boolean hasMonth()
                         throws DecodeException
        Tests whether the field Month has a value.
        Returns:
        whether the field has a value
        Throws:
        DecodeException - if encoded state cannot be decoded
      • setMonthPresent

        public DateAndTime setMonthPresent​(boolean flag)
                                    throws IllegalStateException
        Sets the presence or absence of the field Month.
        Parameters:
        flag - whether the field should be marked as present
        Returns:
        this
        Throws:
        IllegalStateException - if this instance has been marked as read-only
      • hasDay

        public boolean hasDay()
                       throws DecodeException
        Tests whether the field Day has a value.
        Returns:
        whether the field has a value
        Throws:
        DecodeException - if encoded state cannot be decoded
      • setDayPresent

        public DateAndTime setDayPresent​(boolean flag)
                                  throws IllegalStateException
        Sets the presence or absence of the field Day.
        Parameters:
        flag - whether the field should be marked as present
        Returns:
        this
        Throws:
        IllegalStateException - if this instance has been marked as read-only
      • hasHour

        public boolean hasHour()
                        throws DecodeException
        Tests whether the field Hour has a value.
        Returns:
        whether the field has a value
        Throws:
        DecodeException - if encoded state cannot be decoded
      • setHourPresent

        public DateAndTime setHourPresent​(boolean flag)
                                   throws IllegalStateException
        Sets the presence or absence of the field Hour.
        Parameters:
        flag - whether the field should be marked as present
        Returns:
        this
        Throws:
        IllegalStateException - if this instance has been marked as read-only
      • hasMinute

        public boolean hasMinute()
                          throws DecodeException
        Tests whether the field Minute has a value.
        Returns:
        whether the field has a value
        Throws:
        DecodeException - if encoded state cannot be decoded
      • setMinutePresent

        public DateAndTime setMinutePresent​(boolean flag)
                                     throws IllegalStateException
        Sets the presence or absence of the field Minute.
        Parameters:
        flag - whether the field should be marked as present
        Returns:
        this
        Throws:
        IllegalStateException - if this instance has been marked as read-only
      • hasSecond

        public boolean hasSecond()
                          throws DecodeException
        Tests whether the field Second has a value.
        Returns:
        whether the field has a value
        Throws:
        DecodeException - if encoded state cannot be decoded
      • setSecondPresent

        public DateAndTime setSecondPresent​(boolean flag)
                                     throws IllegalStateException
        Sets the presence or absence of the field Second.
        Parameters:
        flag - whether the field should be marked as present
        Returns:
        this
        Throws:
        IllegalStateException - if this instance has been marked as read-only
      • getYear

        public int getYear()
                    throws DecodeException
        Gets the value of the year, in range [0000,9999].
        Returns:
        int representation of the year, in range [0000,9999]
        Throws:
        DecodeException - if encoded state cannot be decoded
      • getMonth

        public int getMonth()
                     throws DecodeException
        Gets the value of the month, in range [1,12].
        Returns:
        int representation of the month, in range [1,12]
        Throws:
        DecodeException - if encoded state cannot be decoded
      • getDay

        public int getDay()
                   throws DecodeException
        Gets the value of the day of the month, in range [1,31].
        Returns:
        int representation of the day of the month, in range [1,31]
        Throws:
        DecodeException - if encoded state cannot be decoded
      • getHour

        public int getHour()
                    throws DecodeException
        Gets the value of the hour, in range [0,23].
        Returns:
        int representation of the hour, in range [0,23]
        Throws:
        DecodeException - if encoded state cannot be decoded
      • getMinute

        public int getMinute()
                      throws DecodeException
        Gets the value of the minute, in range [0,59].
        Returns:
        int representation of the minute, in range [0,59]
        Throws:
        DecodeException - if encoded state cannot be decoded
      • getSecond

        public int getSecond()
                      throws DecodeException
        Gets the value of the second, in range [0,59].
        Returns:
        int representation of the second, in range [0,59]
        Throws:
        DecodeException - if encoded state cannot be decoded
      • copyOf

        public static DateAndTime copyOf​(DateAndTime from)
        Creates a new object of this type, cloning the values of common fields from the given object of any subclass of the same base type.
        Parameters:
        from - the object from which to copy
        Returns:
        DateAndTime
      • getFieldAccessors

        public static FieldAccessor[] getFieldAccessors()
        Gets a new array of the accessors for fields of this type.
        Returns:
        FieldAccessor[]
      • getFieldsMap

        public Map<String,​Object> getFieldsMap​(boolean withAbsents)
        Gets a Map from field-name to field-value for the fields of a sequence. For a field of primitive type, the field-value is boxed. For an absent optional field, the field-value is AbstractFieldsObject.FIELD_ABSENT. For an uninitialised mandatory field, the field-value is AbstractFieldsObject.FIELD_UNINITIALISED. For a field of String type, the encoded form is used if the String cannot be decoded. For a type that represents an OCTET STRING that cannot be decoded, a map from "encodedValue" to the byte array value is returned.
        Specified by:
        getFieldsMap in class AbstractFieldsObject
        Parameters:
        withAbsents - whether to put absent optional fields into Map
        Returns:
        Map from field-name to field-value
      • encodedIsInvalid

        protected boolean encodedIsInvalid()
        Determines whether the encoded form is invalid.
        Returns:
        boolean
      • getEncodedForm

        public byte[] getEncodedForm()
                              throws EncodeException
        Gets the encoded form.
        Returns:
        encoded form
        Throws:
        EncodeException - if encoding fails
      • getContainedLength

        public static int getContainedLength​(byte[] data,
                                             int start,
                                             int len)
        Determines the length of the encoded value that commences at offset start in the given data. The value is not fully decoded and might not be decodable. Values of this type are of variable length, so the given length is returned if the data is long enough, otherwise 0 is returned.
        Parameters:
        data - network-encoded data
        start - index into data where value commences
        len - length in data to be considered
        Returns:
        len, or 0 if encoded value is not long enough
      • getInapEncodedForm

        public byte[] getInapEncodedForm()
        Gets the INAP encoded form of this DateAndTime instance. The INAP encoded form is a BCD-encoded byte array of length 6 in the form YYMMDDHHMMSS.
        Returns:
        the INAP encoded form of this DateAndTime instance
      • getCapEncodedForm

        public byte[] getCapEncodedForm()
        Gets the CAP encoded form of this DateAndTime instance. The CAP encoded form is a BCD-encoded byte array of length 7 in the form YYYYMMDDHHMMSS.
        Returns:
        the CAP encoded form of this DateAndTime instance
      • toStringPretty

        public String toStringPretty()
        Gets a string representation for this object, similar to "30/09/1993 12:15:01".
        Returns:
        a nicely formatted date and time
      • encode

        protected void encode()
                       throws EncodeException
        Computes the encoded form from the decoded form.
        Specified by:
        encode in class AbstractLazyEncodedDataObject<byte[]>
        Throws:
        EncodeException - if Year is not in range [0000,9999], or Month is not in range [1,12], or Day is not in range [1,31], or Hour is not in range [0,23], or Minute is not in range [0,59], or Second is not in range [0,59]