xmlgregoriancalendarimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,796 行 · 第 1/5 页

JAVA
1,796
字号
     * @return result of adding second and fractional second field     */    private BigDecimal getSeconds() {        if (second == DatatypeConstants.FIELD_UNDEFINED) {            return DECIMAL_ZERO;        }        BigDecimal result = BigDecimal.valueOf((long) second);        if (fractionalSecond != null) {            return result.add(fractionalSecond);        } else {            return result;        }    }    /**     * <p>Return millisecond precision of {@link #getFractionalSecond()}.<\p>     *     * <p>This method represents a convenience accessor to infinite     * precision fractional second value returned by     * {@link #getFractionalSecond()}. The returned value is the rounded     * down to milliseconds value of     * {@link #getFractionalSecond()}. When {@link #getFractionalSecond()}     * returns <code>null</code>, this method must return     * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>     *     * <p>Value constraints for this value are summarized in     * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>     *     * @return Millisecond  of this <code>XMLGregorianCalendar</code>.     *     * @see #getFractionalSecond()     * @see #setTime(int, int, int)     */    public int getMillisecond() {        if (fractionalSecond == null) {            return DatatypeConstants.FIELD_UNDEFINED;        } else {            // TODO: Non-optimal solution for now.            // Efficient implementation would only store as BigDecimal            // when needed and millisecond otherwise.            return fractionalSecond.movePointRight(3).intValue();        }    }    /**     * <p>Return fractional seconds.</p>     *     * <p><code>null</code> is returned when this optional field is not defined.</p>     *     * <p>Value constraints are detailed in     * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>     *     * <p>This optional field can only have a defined value when the     * xs:dateTime second field, represented by ({@link #getSecond()},     * does not return {@link DatatypeConstants#FIELD_UNDEFINED}).</p>     *     * @return fractional seconds  of this <code>XMLGregorianCalendar</code>.     *     * @see #getSecond()     * @see #setTime(int, int, int, BigDecimal)     */    public BigDecimal getFractionalSecond() {	   return fractionalSecond;    }    // setters    /**     * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>     *     * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>     *     * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.     *     * @throws IllegalArgumentException if <code>year</code> parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setYear(BigInteger year) {        if (year == null) {            this.eon = null;            this.year = DatatypeConstants.FIELD_UNDEFINED;        } else {            BigInteger temp = year.remainder(BILLION);            this.year = temp.intValue();            setEon(year.subtract(temp));        }    }    /**     * <p>Set year of XSD <code>dateTime</code> year field.</p>     *     * <p>Unset this field by invoking the setter with a parameter value of     * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>     *     * <p>Note: if the absolute value of the <code>year</code> parameter     * is less than 10^9, the eon component of the XSD year field is set to     * <code>null</code> by this method.</p>     *     * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.     *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.     */    public void setYear(int year) {        if (year == DatatypeConstants.FIELD_UNDEFINED) {            this.year = DatatypeConstants.FIELD_UNDEFINED;            this.eon = null;        } else if (Math.abs(year) < BILLION.intValue()) {            this.year = year;            this.eon = null;        } else {            BigInteger theYear = BigInteger.valueOf((long) year);            BigInteger remainder = theYear.remainder(BILLION);            this.year = remainder.intValue();            setEon(theYear.subtract(remainder));        }    }    /**     * <p>Set high order part of XSD <code>dateTime</code> year field.</p>     *     * <p>Unset this field by invoking the setter with a parameter value of     * <code>null</code>.</p>     *     * @param eon value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.     */    private void setEon(BigInteger eon) {        if (eon != null && eon.compareTo(BigInteger.ZERO) == 0) {            // Treat ZERO as field being undefined.            this.eon = null;        } else {            this.eon = eon;        }    }    /**     * <p>Set month.</p>     *     * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>     *     * @param month value constraints summarized in <a href="#datetimefield-month">month field of date/time field mapping table</a>.     *     * @throws IllegalArgumentException if <code>month</code> parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setMonth(int month) {        if(month<DatatypeConstants.JANUARY || DatatypeConstants.DECEMBER<month)            if(month!=DatatypeConstants.FIELD_UNDEFINED)                invalidFieldValue(MONTH, month);        this.month = month;    }    /**     * <p>Set days in month.</p>     *     * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>     *     * @param day value constraints summarized in <a href="#datetimefield-day">day field of date/time field mapping table</a>.     *     * @throws IllegalArgumentException if <code>day</code> parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setDay(int day) {        if(day<1 || 31<day)            if(day!=DatatypeConstants.FIELD_UNDEFINED)                invalidFieldValue(DAY,day);        this.day = day;    }    /**     * <p>Set the number of minutes in the timezone offset.</p>     *     * <p>Unset this field by invoking the setter with a parameter value of {@link DatatypeConstants#FIELD_UNDEFINED}.</p>     *     * @param offset value constraints summarized in <a href="#datetimefield-timezone">     *   timezone field of date/time field mapping table</a>.     *     * @throws IllegalArgumentException if <code>offset</code> parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setTimezone(int offset) {	    if(offset<-14*60 || 14*60<offset)            if(offset!=DatatypeConstants.FIELD_UNDEFINED)                invalidFieldValue(TIMEZONE,offset);        this.timezone = offset;    }    /**     * <p>Set time as one unit.</p>     *     * @param hour value constraints are summarized in     * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.     * @param minute value constraints are summarized in     * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.     * @param second value constraints are summarized in     * <a href="#datetimefield-second">second field of date/time field mapping table</a>.     *     * @see #setTime(int, int, int, BigDecimal)     *     * @throws IllegalArgumentException if any parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setTime(int hour, int minute, int second) {        setTime(hour, minute, second, null);    }    private void invalidFieldValue(int field, int value) {        throw new IllegalArgumentException(            DatatypeMessageFormatter.formatMessage(null, "InvalidFieldValue",                new Object[]{ new Integer(value), FIELD_NAME[field]})        );    }        private void testHour() {        // http://www.w3.org/2001/05/xmlschema-errata#e2-45        if (getHour() == 24) {            if (getMinute() != 0                    || getSecond() != 0) {                invalidFieldValue(HOUR, getHour());            }        }    }    public void setHour(int hour) {        setHour(hour, true);    }    private void setHour(int hour, boolean validate) {        if (hour < 0 || hour > 24) {            if (hour != DatatypeConstants.FIELD_UNDEFINED) {                invalidFieldValue(HOUR, hour);            }        }        this.hour = hour;        if (validate) {            testHour();        }    }    public void setMinute(int minute) {        if(minute<0 || 59<minute)            if(minute!=DatatypeConstants.FIELD_UNDEFINED)                invalidFieldValue(MINUTE, minute);        this.minute = minute;    }    public void setSecond(int second) {        if(second<0 || 60<second)   // leap second allows for 60            if(second!=DatatypeConstants.FIELD_UNDEFINED)                invalidFieldValue(SECOND, second);        this.second  = second;    }    /**     * <p>Set time as one unit, including the optional infinite precison     * fractional seconds.</p>     *     * @param hour value constraints are summarized in     * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.     * @param minute value constraints are summarized in     * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.     * @param second value constraints are summarized in     * <a href="#datetimefield-second">second field of date/time field mapping table</a>.     * @param fractional value of <code>null</code> indicates this optional     *                   field is not set.     *     * @throws IllegalArgumentException if any parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setTime(            int hour,            int minute,            int second,            BigDecimal fractional) {        setHour(hour, false);        setMinute(minute);        if (second != 60) {            setSecond(second);        } else if ((hour == 23 && minute == 59) || (hour == 0 && minute == 0)) {            setSecond(second);        } else {            invalidFieldValue(SECOND, second);        }        setFractionalSecond(fractional);        // must test hour after setting seconds        testHour();    }    /**     * <p>Set time as one unit, including optional milliseconds.</p>     *     * @param hour value constraints are summarized in     * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.     * @param minute value constraints are summarized in     * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.     * @param second value constraints are summarized in     * <a href="#datetimefield-second">second field of date/time field mapping table</a>.     * @param millisecond value of {@link DatatypeConstants#FIELD_UNDEFINED} indicates this     *                    optional field is not set.     *     * @throws IllegalArgumentException if any parameter is     * outside value constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public void setTime(int hour, int minute, int second, int millisecond) {        setHour(hour, false);        setMinute(minute);        if (second != 60) {            setSecond(second);        } else if ((hour == 23 && minute == 59) || (hour == 0 && minute == 0)) {            setSecond(second);        } else {            invalidFieldValue(SECOND, second);        }        setMillisecond(millisecond);        // must test hour after setting seconds        testHour();    }    // comparisons    /**     * <p>Compare two instances of W3C XML Schema 1.0 date/time datatypes     * according to partial order relation defined in     * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">W3C XML Schema 1.0 Part 2, Section 3.2.7.3,     * <i>Order relation on dateTime</i></a>.</p>     *     * <p><code>xsd:dateTime</code> datatype field mapping to accessors of     * this class are defined in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.</p>     *     * @param rhs instance of <code>XMLGregorianCalendar</code> to compare     *     * @return the relationship between <code>lhs</code> and <code>rhs</code> as     *   {@link DatatypeConstants#LESSER},     *   {@link DatatypeConstants#EQUAL},     *   {@link DatatypeConstants#GREATER} or     *   {@link DatatypeConstants#INDETERMINATE}.     *     * @throws NullPointerException if <code>lhs</code> or <code>rhs</code>     * parameters are null.     */    public int compare(XMLGregorianCalendar rhs) {

⌨️ 快捷键说明

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