⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeattribute.java

📁 sunxacml源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            // if we had a negative value then we need to shift by a day
            if (timeGMT < 0)
                timeGMT += DateAttribute.MILLIS_PER_DAY;
        }
    }

    /**
     * Returns a new <code>TimeAttribute</code> that represents
     * the xs:time at a particular DOM node.
     *
     * @param root the <code>Node</code> that contains the desired value
     * @return a new <code>TimeAttribute</code> representing the
     *         appropriate value (null if there is a parsing error)
     */
    public static TimeAttribute getInstance(Node root)
        throws ParsingException, NumberFormatException, ParseException
    {
        return getInstance(root.getFirstChild().getNodeValue());
    }

    /**
     * Returns a new <code>TimeAttribute</code> that represents
     * the xs:time value indicated by the string provided.
     *
     * @param value a string representing the desired value
     * @return a new <code>TimeAttribute</code> representing the
     *         desired value (null if there is a parsing error)
     * @throws ParsingException if any problems occurred while parsing
     */
    public static TimeAttribute getInstance(String value)
        throws ParsingException, NumberFormatException, ParseException
    {
        // Prepend date string for Jan 1 1970 and use the
        // DateTimeAttribute parsing code.

        value = "1970-01-01T" + value;

        DateTimeAttribute dateTime = DateTimeAttribute.getInstance(value);

        // if there was no explicit TZ provided, then we want to make sure
        // the that the defaulting is done correctly, especially since 1/1/70
        // is always out of daylight savings time

        Date dateValue = dateTime.getValue();
        int defaultedTimeZone = dateTime.getDefaultedTimeZone();
        if (dateTime.getTimeZone() == TZ_UNSPECIFIED) {
            TimeZone localTZ = TimeZone.getDefault();
            int newDefTimeZone =
                DateTimeAttribute.getDefaultTZOffset(new Date());
            dateValue = new Date(dateValue.getTime() -
                                 (newDefTimeZone - defaultedTimeZone) *
                                 DateAttribute.MILLIS_PER_MINUTE);
            defaultedTimeZone = newDefTimeZone;
        }

        return new TimeAttribute(dateValue,
                                 dateTime.getNanoseconds(),
                                 dateTime.getTimeZone(),
                                 defaultedTimeZone);
    }

    /**
     * Gets the time represented by this object. The return
     * value is a <code>Date</code> object representing the
     * specified time down to second resolution with a date
     * of January 1, 1970. Subsecond values are handled by the
     * {@link #getNanoseconds getNanoseconds} method.
     *
     * @return a <code>Date</code> object representing the
     *         time represented by this object
     */
    public Date getValue() {
        return new Date(timeGMT);
    }

    /**
     * Gets the number of milliseconds since midnight GMT that this attribute
     * value represents. This is the same time returned by
     * <code>getValue</code>, and likewise the milliseconds are provided
     * with second resolution.
     *
     * @return milliseconds since midnight GMT
     */
    public long getMilliseconds() {
        return timeGMT;
    }

    /**
     * Gets the nanoseconds of this object.
     *
     * @return the number of nanoseconds
     */
    public int getNanoseconds() {
        return nanoseconds;
    }

    /**
     * Gets the time zone of this object (or TZ_UNSPECIFIED if
     * unspecified).
     *
     * @return the offset to GMT in minutes (positive or negative)
     */
    public int getTimeZone() {
        return timeZone;
    }

    /**
     * Gets the time zone actually used for this object (if it was
     * originally unspecified, the default time zone used).
     *
     * @return the offset to GMT in minutes (positive or negative)
     */
    public int getDefaultedTimeZone() {
        return defaultedTimeZone;
    }

    /**
     * Returns true if the input is an instance of this class and if its
     * value equals the value contained in this class.
     *
     * @param o the object to compare
     *
     * @return true if this object and the input represent the same value
     */
    public boolean equals(Object o) {
        if (! (o instanceof TimeAttribute))
            return false;

        TimeAttribute other = (TimeAttribute)o;

        return (timeGMT == other.timeGMT &&
                (nanoseconds == other.nanoseconds));
    }

    /**
     * Returns the hashcode value used to index and compare this object with
     * others of the same type. Typically this is the hashcode of the backing
     * data object.
     *
     * @return the object's hashcode value
     */
    public int hashCode() {
        // the standard Date hashcode is used here...
        int hashCode = (int)(timeGMT ^ (timeGMT >>> 32));

        // ...but both the timeGMT and the nanoseconds fields are considered
        // by the equals method, so it's best if the hashCode is derived
        // from both of those fields.
        hashCode = (31 * hashCode) + nanoseconds;

        return hashCode;
    }

    /**
     * Converts to a String representation.
     *
     * @return the String representation
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("TimeAttribute: [\n");
        
        // calculate the GMT value of this time
        long secsGMT = timeGMT / 1000;
        long minsGMT = secsGMT / 60;
        secsGMT = secsGMT % 60;
        long hoursGMT = minsGMT / 60;
        minsGMT = minsGMT % 60;

        // put the right number of zeros in place
        String hoursStr = (hoursGMT < 10) ? "0" + hoursGMT : "" + hoursGMT;
        String minsStr = (minsGMT < 10) ? "0" + minsGMT : "" + minsGMT;
        String secsStr = (secsGMT < 10) ? "0" + secsGMT : "" + secsGMT;
        
        sb.append("  Time GMT: " + hoursStr + ":" + minsStr + ":" + secsStr);
        sb.append("  Nanoseconds: " + nanoseconds);
        sb.append("  TimeZone: " + timeZone);
        sb.append("  Defaulted TimeZone: " + defaultedTimeZone);
        sb.append("]");

        return sb.toString();
    }

    /**
     * Encodes the value in a form suitable for including in XML data like
     * a request or an obligation. This returns a time value that could in
     * turn be used by the factory to create a new instance with the same
     * value.
     *
     * @return a <code>String</code> form of the value
     */
    public String encode() {
        if (encodedValue != null)
            return encodedValue;

        // "hh:mm:ss.sssssssss+hh:mm".length() = 27
        StringBuffer buf = new StringBuffer(27);

        // get the correct time for the timezone being used
        int millis = (int)timeGMT;
        if (timeZone == TZ_UNSPECIFIED)
            millis += (defaultedTimeZone * DateAttribute.MILLIS_PER_MINUTE);
        else
            millis += (timeZone * DateAttribute.MILLIS_PER_MINUTE);

        if (millis < 0) {
            millis += DateAttribute.MILLIS_PER_DAY;
        } else if (millis >= DateAttribute.MILLIS_PER_DAY) {
            millis -= DateAttribute.MILLIS_PER_DAY;
        }

        // now generate the time string
        int hour = millis / DateAttribute.MILLIS_PER_HOUR;
        millis = millis % DateAttribute.MILLIS_PER_HOUR;
        buf.append(DateAttribute.zeroPadInt(hour, 2));
        buf.append(':');
        int minute = millis / DateAttribute.MILLIS_PER_MINUTE;
        millis = millis % DateAttribute.MILLIS_PER_MINUTE;
        buf.append(DateAttribute.zeroPadInt(minute, 2));
        buf.append(':');
        int second = millis / DateAttribute.MILLIS_PER_SECOND;
        buf.append(DateAttribute.zeroPadInt(second, 2));

        // add any nanoseconds
        if (nanoseconds != 0) {
            buf.append('.');
            buf.append(DateAttribute.zeroPadInt(nanoseconds, 9));
        }

        // if there is a specified timezone, then include that in the encoding
        if (timeZone != TZ_UNSPECIFIED) {
            int tzNoSign = timeZone;
            if (timeZone < 0) {
                tzNoSign = -tzNoSign;
                buf.append('-');
            } else
                buf.append('+');
            int tzHours = tzNoSign / 60;
            buf.append(DateAttribute.zeroPadInt(tzHours, 2));
            buf.append(':');
            int tzMinutes = tzNoSign % 60;
            buf.append(DateAttribute.zeroPadInt(tzMinutes, 2));
        }

        // remember the encoding for later
        encodedValue = buf.toString();

        return encodedValue;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -