xmlgregoriancalendarimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,796 行 · 第 1/5 页
JAVA
1,796 行
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(); // normalizing to UTC time negates the timezone offset before // addition. minutes = -minutes; Duration d = new DurationImpl(minutes >= 0, // isPositive 0, //years 0, //months 0, //days 0, //hours minutes < 0 ? -minutes : minutes, // absolute 0 //seconds ); result.add(d); // set to zulu UTC time. result.setTimezone(0); return result; } /** * * <p>Implements Step B from http://www.w3.org/TR/xmlschema-2/#dateTime-order </p> * @param P calendar instance with normalized timezone offset or * having same timezone as Q * @param Q calendar instance with normalized timezone offset or * having same timezone as P * * @return result of comparing P and Q, value of * {@link DatatypeConstants#EQUAL}, * {@link DatatypeConstants#LESSER}, * {@link DatatypeConstants#GREATER} or * {@link DatatypeConstants#INDETERMINATE}. */ private static int internalCompare(XMLGregorianCalendar P, XMLGregorianCalendar Q) { int result; // compare Year. if (P.getEon() == Q.getEon()) { // Eon field is only equal when null. // optimized case for comparing year not requiring eon field. result = compareField(P.getYear(), Q.getYear()); if (result != DatatypeConstants.EQUAL) { return result; } } else { result = compareField(P.getEonAndYear(), Q.getEonAndYear()); if (result != DatatypeConstants.EQUAL) { return result; } } result = compareField(P.getMonth(), Q.getMonth()); if (result != DatatypeConstants.EQUAL) { return result; } result = compareField(P.getDay(), Q.getDay()); if (result != DatatypeConstants.EQUAL) { return result; } result = compareField(P.getHour(), Q.getHour()); if (result != DatatypeConstants.EQUAL) { return result; } result = compareField(P.getMinute(), Q.getMinute()); if (result != DatatypeConstants.EQUAL) { return result; } result = compareField(P.getSecond(), Q.getSecond()); if (result != DatatypeConstants.EQUAL) { return result; } result = compareField(P.getFractionalSecond(), Q.getFractionalSecond()); return result; } /** * <p>Implement Step B from * http://www.w3.org/TR/xmlschema-2/#dateTime-order.</p> */ private static int compareField(int Pfield, int Qfield) { if (Pfield == Qfield) { //fields are either equal in value or both undefined. // Step B. 1.1 AND optimized result of performing 1.1-1.4. return DatatypeConstants.EQUAL; } else { if (Pfield == DatatypeConstants.FIELD_UNDEFINED || Qfield == DatatypeConstants.FIELD_UNDEFINED) { // Step B. 1.2 return DatatypeConstants.INDETERMINATE; } else { // Step B. 1.3-4. return (Pfield < Qfield ? DatatypeConstants.LESSER : DatatypeConstants.GREATER); } } } private static int compareField(BigInteger Pfield, BigInteger Qfield) { if (Pfield == null) { return (Qfield == null ? DatatypeConstants.EQUAL : DatatypeConstants.INDETERMINATE); } if (Qfield == null) { return DatatypeConstants.INDETERMINATE; } return Pfield.compareTo(Qfield); } private static int compareField(BigDecimal Pfield, BigDecimal Qfield) { // optimization. especially when both arguments are null. if (Pfield == Qfield) { return DatatypeConstants.EQUAL; } if (Pfield == null) { Pfield = DECIMAL_ZERO; } if (Qfield == null) { Qfield = DECIMAL_ZERO; } return Pfield.compareTo(Qfield); } /** * <p>Indicates whether parameter <code>obj</code> is "equal to" this one.</p> * * @param obj to compare. * * @return <code>true</code> when <code>compare(this,(XMLGregorianCalendar)obj) == EQUAL.</code>. */ public boolean equals(Object obj) { if (obj == null || !(obj instanceof XMLGregorianCalendar)) { return false; } return compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL; } /** * <p>Returns a hash code consistent with the definition of the equals method.</p> * * @return hash code of this object. */ public int hashCode() { // Following two dates compare to EQUALS since in different timezones. // 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00 // // Must ensure both instances generate same hashcode by normalizing // this to UTC timezone. int timezone = getTimezone(); if (timezone == DatatypeConstants.FIELD_UNDEFINED) { timezone = 0; } XMLGregorianCalendar gc = this; if (timezone != 0) { gc = this.normalizeToTimezone(getTimezone()); } return gc.getYear() + gc.getMonth() + gc.getDay() + gc.getHour() + gc.getMinute() + gc.getSecond(); } /** * <p>Constructs a new XMLGregorianCalendar object by * parsing its lexical string representation as defined in * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1, * <i>Lexical Representation</i>.</a></p> * * <p>The string representation may not have any leading and trailing whitespaces.</p> * * <p>The parsing is done field by field so that * the following holds for any lexically correct string x:</p> * <pre> * new XMLGregorianCalendar(x).toXMLFormat().equals(x) * </pre> * Except for the noted lexical/canonical representation mismatches * listed in <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45"> * XML Schema 1.0 errata, Section 3.2.7.2</a>. * * <p>Returns a non-null valid XMLGregorianCalendar object that holds the value * indicated by the lexicalRepresentation parameter.</p> * * @param lexicalRepresentation Lexical representation of one the 8 XML Schema calendar datatypes. * * @return <code>XMLGregorianCalendar</code> created from parsing <code>lexicalRepresentation</code> parameter. * * @throws IllegalArgumentException * If the given string does not conform to the aforementioned * specification. * @throws NullPointerException * If the given string is null. */ public static XMLGregorianCalendar parse(String lexicalRepresentation) { return new XMLGregorianCalendarImpl(lexicalRepresentation); } /** * <p>Return the lexical representation of <code>this</code> instance. * The format is specified in * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1, * <i>Lexical Representation</i>".</a></p> * * <p>Specific target lexical representation format is determined by * {@link #getXMLSchemaType()}.</p> * * @return XML, as <code>String</code>, representation of this <code>XMLGregorianCalendar</code> * * @throws java.lang.IllegalStateException if the combination of set fields * does not match one of the eight defined XML Schema builtin date/time datatypes. */ public String toXMLFormat() { QName typekind = getXMLSchemaType(); String formatString = null; // Fix 4971612: invalid SCCS macro substitution in data string // no %{alpha}% to avoid SCCS macro substitution if (typekind == DatatypeConstants.DATETIME) { formatString = "%Y-%M-%DT%h:%m:%s" + "%z"; } else if (typekind == DatatypeConstants.DATE) { formatString = "%Y-%M-%D" + "%z"; } else if (typekind == DatatypeConstants.TIME) { formatString = "%h:%m:%s" + "%z"; } else if (typekind == DatatypeConstants.GMONTH) { formatString = "--%M" + "%z"; } else if (typekind == DatatypeConstants.GDAY) { formatString = "---%D" + "%z"; } else if (typekind == DatatypeConstants.GYEAR) { formatString = "%Y" + "%z"; } else if (typekind == DatatypeConstants.GYEARMONTH) { formatString = "%Y-%M" + "%z"; } else if (typekind == DatatypeConstants.GMONTHDAY) { formatString = "--%M-%D" + "%z"; } return format(formatString); } /** * <p>Return the name of the XML Schema date/time type that this instance * maps to. Type is computed based on fields that are set.</p> * * <table border="2" rules="all" cellpadding="2"> * <thead> * <tr> * <th align="center" colspa
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?