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

📄 datetimeattribute.java

📁 sunxacml源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            nanoseconds = Integer.parseInt(nanoString);

            // Remove the fractional seconds from the string.
            value = value.substring(0, dotIndex) +
                value.substring(secondsEnd, value.length());
        }

        // this is the code that may trow a ParseException
        if (hasTimeZone) {
            // Strip off the purported time zone and make sure what's
            // left is a valid unzoned date and time (by parsing in GMT).
            // If so, reformat the time zone by stripping out the colon
            // and parse the revised string with the timezone parser.
            
            len = value.length();
            
            Date gmtValue = strictParse(zoneParser,
                                        value.substring(0,len-6) + "+0000");
            value = value.substring(0, len-3) +
                value.substring(len-2, len);
            dateValue = strictParse(zoneParser, value);
            timeZone =
                (int) (gmtValue.getTime() - dateValue.getTime());
            timeZone = timeZone / 60000;
            defaultedTimeZone = timeZone;
        } else {
            // No funny business. This must be a simple date and time.
            dateValue = strictParse(simpleParser, value);
            timeZone = TZ_UNSPECIFIED;
            // Figure out what time zone was used.
            Date gmtValue = strictParse(zoneParser, value + "+0000");
            defaultedTimeZone =
                (int) (gmtValue.getTime() - dateValue.getTime());
            defaultedTimeZone = defaultedTimeZone / 60000;
        }

        // If parsing went OK, create a new DateTimeAttribute object and
        // return it.

        DateTimeAttribute attr = new DateTimeAttribute(dateValue, nanoseconds,
                                                       timeZone,
                                                       defaultedTimeZone);
        return attr;
    }

    /**
     * Parse a String using a DateFormat parser, requiring that
     * the entire String be consumed by the parser. On success,
     * return a Date. On failure, throw a ParseException.
     * <p>
     * Synchronize on the parser object when using it, since we
     * assume they're the shared static objects in this class.
     */
    private static Date strictParse(DateFormat parser, String str)
        throws ParseException {
        ParsePosition pos = new ParsePosition(0);
        Date ret;
        synchronized (parser) {
            ret = parser.parse(str, pos);
        }
        if (pos.getIndex() != str.length())
            throw new ParseException("", 0);
        return ret;
    }

    /**
     * Initialize the parser objects.
     */
    private static void initParsers() {
        // If simpleParser is already set, we're done.
        if (simpleParser != null)
            return;

        // Make sure that identifierURI is not null
        if (earlyException != null)
            throw earlyException;

        // Synchronize on identifierURI while initializing parsers
        // so we don't end up using a half-way initialized parser
        synchronized (identifierURI) {
            // This simple parser has no time zone
            simpleParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            simpleParser.setLenient(false);

            // This parser has a four digit offset to GMT with sign
            zoneParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            zoneParser.setLenient(false);
        }
    }

    /**
     * Gets the date and time represented by this object. The return
     * value is a <code>Date</code> object representing the
     * specified date and time down to second resolution.
     * Subsecond values are handled by the
     * {@link #getNanoseconds getNanoseconds} method.
     * <p>
     * <b>NOTE:</b> The <code>Date</code> object is cloned before it
     * is returned to avoid unauthorized changes.
     *
     * @return a <code>Date</code> object representing the date and
     *         time represented by this object
     */
    public Date getValue() {
        return (Date) value.clone();
    }

    /**
     * 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.
     * <p>
     * Two <code>DateTimeAttribute</code>s are equal if and only if the
     * dates and times represented are identical (down to the nanosecond).
     *
     * @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 DateTimeAttribute))
            return false;

        DateTimeAttribute other = (DateTimeAttribute)o;

        // Since the value field is normalized into GMT, this is a
        // good way to compare.
        return (value.equals(other.value) &&
                (nanoseconds == other.nanoseconds));
    }

    /**
     * Returns the hashcode value used to index and compare this object with
     * others of the same type.
     *
     * @return the object's hashcode value
     */
    public int hashCode() {
        // Both the value field and the nanoseconds field are considered
        // by the equals method, so it's best if the hashCode is derived
        // from both of those fields.
        int hashCode = value.hashCode();
        hashCode = 31*hashCode + nanoseconds;
        return hashCode;
    }

    /**
     * Converts to a String representation.
     *
     * @return the String representation
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("DateTimeAttribute: [\n");
        sb.append("  Date: " + value + " local time");
        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 must return a 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;

        if (timeZone == TZ_UNSPECIFIED) {
            // If no time zone was specified, format Date value in
            // local time with no time zone string.
            initParsers();
            synchronized (simpleParser) {
                encodedValue = simpleParser.format(value);
            }
            if (nanoseconds != 0) {
                encodedValue = encodedValue + "." +
                    DateAttribute.zeroPadInt(nanoseconds, 9);
            }
        } else {
            // If a time zone was specified, don't use SimpleParser
            // because it can only format dates in the local (default)
            // time zone. And the offset between that time zone and the
            // time zone we need to display can vary in complicated ways.

            // Instead, do it ourselves using our formatDateWithTZ method.
            encodedValue = formatDateTimeWithTZ();
        }
        return encodedValue;
    }

    /**
     * Encodes the value of this object as an xsi:dateTime.
     * Only for use when the time zone is specified.
     *
     * @return a <code>String</code> form of the value
     */
    private String formatDateTimeWithTZ() {
        if (gmtCalendar == null) {
            TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");

            // Locale doesn't make much difference here. We don't use
            // any of the strings in the Locale and we don't do anything
            // that depends on week count conventions. We use the US
            // locale because it's always around and it ensures that we
            // will always get a Gregorian calendar, which is necessary
            // for compliance with ISO 8501.
            gmtCalendar = Calendar.getInstance(gmtTimeZone, Locale.US);
        }

        // "YYYY-MM-DDThh:mm:ss.sssssssss+hh:mm".length() = 35
        // Length may be longer if years < -999 or > 9999
        StringBuffer buf = new StringBuffer(35);

        synchronized (gmtCalendar) {
            // Start with the proper time in GMT.
            gmtCalendar.setTime(value);
            // Bump by the timeZone, since we're going to be extracting
            // the value in GMT
            gmtCalendar.add(Calendar.MINUTE, timeZone);

            // Now, assemble the string
            int year = gmtCalendar.get(Calendar.YEAR);
            buf.append(DateAttribute.zeroPadInt(year, 4));
            buf.append('-');
            // JANUARY is 0
            int month = gmtCalendar.get(Calendar.MONTH) + 1;
            buf.append(DateAttribute.zeroPadInt(month, 2));
            buf.append('-');
            int dom = gmtCalendar.get(Calendar.DAY_OF_MONTH);
            buf.append(DateAttribute.zeroPadInt(dom, 2));
            buf.append('T');
            int hour = gmtCalendar.get(Calendar.HOUR_OF_DAY);
            buf.append(DateAttribute.zeroPadInt(hour, 2));
            buf.append(':');
            int minute = gmtCalendar.get(Calendar.MINUTE);
            buf.append(DateAttribute.zeroPadInt(minute, 2));
            buf.append(':');
            int second = gmtCalendar.get(Calendar.SECOND);
            buf.append(DateAttribute.zeroPadInt(second, 2));
        }

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

        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));

        return buf.toString();
    }

    /**
     * Gets the offset in minutes between the default time zone and
     * UTC for the specified date.
     *
     * @param the <code>Date</code> whose offset is desired
     * @return the offset in minutes
     */
    static int getDefaultTZOffset(Date date) {
        int offset = TimeZone.getDefault().getOffset(date.getTime());
        offset = offset / DateAttribute.MILLIS_PER_MINUTE;
        return offset;
    }

    /**
     * Combines a number of nanoseconds with a <code>Date</code>
     * so that the Date has no fractional seconds and the number
     * of nanoseconds is non-negative and less than a second.
     * <p>
     * <b>WARNING</b>: This function changes the value stored in
     * the date parameter!
     *
     * @param date the <code>Date</code> to be combined
     *             (<b>value may be modified!</b>)
     * @param nanos the nanoseconds to be combined
     * @return the resulting number of nanoseconds
     */
    static int combineNanos(Date date, int nanoseconds) {
        long millis = date.getTime();
        int milliCarry = (int) (millis % DateAttribute.MILLIS_PER_SECOND);

        // If nothing needs fixing, get out quick
        if ((milliCarry == 0) && (nanoseconds > 0)
            && (nanoseconds < DateAttribute.NANOS_PER_SECOND))
            return nanoseconds;

        // Remove any non-zero milliseconds from the date.
        millis -= milliCarry;
        // Add them into the nanoseconds.
        long nanoTemp = nanoseconds;
        nanoTemp += milliCarry * DateAttribute.NANOS_PER_MILLI;
        // Get the nanoseconds that represent fractional seconds.
        // This we'll return.
        int nanoResult = (int) (nanoTemp % DateAttribute.NANOS_PER_SECOND);
        // Get nanoseconds that represent whole seconds.
        nanoTemp -= nanoResult;
        // Convert that to milliseconds and add it back to the date.
        millis += nanoTemp / DateAttribute.NANOS_PER_MILLI;
        date.setTime(millis);

        return nanoResult;
    }
}

⌨️ 快捷键说明

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