xmlgregoriancalendarimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,872 行 · 第 1/5 页
JAVA
1,872 行
} 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) { checkFieldValueConstraint(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) { checkFieldValueConstraint(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) { checkFieldValueConstraint(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 checkFieldValueConstraint(int field, int value) throws IllegalArgumentException { if ((value < MIN_FIELD_VALUE[field] && value != DatatypeConstants.FIELD_UNDEFINED) || value > MAX_FIELD_VALUE[field]) { /** throw new IllegalArgumentException("invalid value " + value + " for " + FIELD_NAME[field] + " field"); */ throw new IllegalArgumentException( DatatypeMessageFormatter.formatMessage(null, "InvalidFieldValue", new Object[]{ new Integer(value), FIELD_NAME[field]}) ); } } public void setHour(int hour) { checkFieldValueConstraint(HOUR, hour); this.hour = hour; } public void setMinute(int minute) { checkFieldValueConstraint(MINUTE, minute); this.minute = minute; } public void setSecond(int second) { checkFieldValueConstraint(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); setMinute(minute); setSecond(second); setFractionalSecond(fractional); } /** * <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); setMinute(minute); setSecond(second); setMillisecond(millisecond); } // 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) { XMLGregorianCalendar lhs = this; int result = DatatypeConstants.INDETERMINATE; XMLGregorianCalendarImpl P = (XMLGregorianCalendarImpl) lhs; XMLGregorianCalendarImpl Q = (XMLGregorianCalendarImpl) rhs; if (P.getTimezone() == Q.getTimezone()) { // Optimization: // both instances are in same timezone or // both are FIELD_UNDEFINED. // Avoid costly normalization of timezone to 'Z' time. return internalCompare(P, Q); } else if (P.getTimezone() != DatatypeConstants.FIELD_UNDEFINED && Q.getTimezone() != DatatypeConstants.FIELD_UNDEFINED) { // Both instances have different timezones. // Normalize to UTC time and compare. P = (XMLGregorianCalendarImpl) P.normalize(); Q = (XMLGregorianCalendarImpl) Q.normalize(); return internalCompare(P, Q); } else if (P.getTimezone() != DatatypeConstants.FIELD_UNDEFINED) { if (P.getTimezone() != 0) { P = (XMLGregorianCalendarImpl) P.normalize(); } // C. step 1 XMLGregorianCalendar MinQ = Q.normalizeToTimezone(DatatypeConstants.MIN_TIMEZONE_OFFSET); result = internalCompare(P, MinQ); if (result == DatatypeConstants.LESSER) { return result; } // C. step 2 XMLGregorianCalendar MaxQ = Q.normalizeToTimezone(DatatypeConstants.MAX_TIMEZONE_OFFSET); result = internalCompare(P, MaxQ); if (result == DatatypeConstants.GREATER) { return result; } else { // C. step 3 return DatatypeConstants.INDETERMINATE; } } else { // Q.getTimezone() != DatatypeConstants.FIELD_UNDEFINED // P has no timezone and Q does. if (Q.getTimezone() != 0 ) { Q = (XMLGregorianCalendarImpl) Q.normalizeToTimezone(Q.getTimezone()); } // D. step 1 XMLGregorianCalendar MaxP = P.normalizeToTimezone(DatatypeConstants.MAX_TIMEZONE_OFFSET); result = internalCompare(MaxP, Q); if (result == DatatypeConstants.LESSER) { return result; } // D. step 2 XMLGregorianCalendar MinP = P.normalizeToTimezone(DatatypeConstants.MIN_TIMEZONE_OFFSET); result = internalCompare(MinP, Q); if (result == DatatypeConstants.GREATER) { return result; } else { // D. step 3 return DatatypeConstants.INDETERMINATE; } } } /** * <p>Normalize this instance to UTC.</p> * * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p> * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p> */ public XMLGregorianCalendar normalize() { XMLGregorianCalendar normalized = normalizeToTimezone(timezone); // if timezone was undefined, leave it undefined if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) { normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED); } // if milliseconds was undefined, leave it undefined if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) { normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED); } return normalized; } /** * <p>Normalize this instance to UTC.</p> * * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p> * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p> */ private XMLGregorianCalendar normalizeToTimezone(int timezone) { int minutes = timezone; XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?