xmlgregoriancalendarimpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,872 行 · 第 1/5 页

JAVA
1,872
字号
		// 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) {	   boolean result = false;	   if (obj instanceof XMLGregorianCalendar) {	       result = compare((XMLGregorianCalendar) obj) == DatatypeConstants.EQUAL;	   }	   return result;    }    /**     * <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;	if (typekind == DatatypeConstants.DATETIME) {	    formatString = "%Y-%M-%DT%h:%m:%s"+ "%z";	} else if (typekind == DatatypeConstants.DATE) {	    // Fix 4971612: invalid SCCS macro substitution in data string	    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) {	    // Fix 4971612: invalid SCCS macro substitution in data string	    formatString = "---%D" + "%z";	} else if (typekind == DatatypeConstants.GYEAR) {	    formatString = "%Y" + "%z";	} else if (typekind == DatatypeConstants.GYEARMONTH) {	    // Fix 4971612: invalid SCCS macro substitution in data string	    formatString = "%Y-%M" + "%z";	} else if (typekind == DatatypeConstants.GMONTHDAY) {	    // Fix 4971612: invalid SCCS macro substitution in data string	    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" colspan="7">     *         Required fields for XML Schema 1.0 Date/Time Datatypes.<br/>     *         <i>(timezone is optional for all date/time datatypes)</i>     *       </th>     *     </tr>     *   </thead>     *   <tbody>     *     <tr>     *       <td>Datatype</td>     *       <td>year</td>     *       <td>month</td>     *       <td>day</td>     *       <td>hour</td>     *       <td>minute</td>     *       <td>second</td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#DATETIME}</td>     *       <td>X</td>     *       <td>X</td>     *       <td>X</td>     *       <td>X</td>     *       <td>X</td>     *       <td>X</td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#DATE}</td>     *       <td>X</td>     *       <td>X</td>     *       <td>X</td>     *       <td></td>     *       <td></td>     *       <td></td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#TIME}</td>     *       <td></td>     *       <td></td>     *       <td></td>     *       <td>X</td>     *       <td>X</td>     *       <td>X</td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#GYEARMONTH}</td>     *       <td>X</td>     *       <td>X</td>     *       <td></td>     *       <td></td>     *       <td></td>     *       <td></td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#GMONTHDAY}</td>     *       <td></td>     *       <td>X</td>     *       <td>X</td>     *       <td></td>     *       <td></td>     *       <td></td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#GYEAR}</td>     *       <td>X</td>     *       <td></td>     *       <td></td>     *       <td></td>     *       <td></td>     *       <td></td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#GMONTH}</td>     *       <td></td>     *       <td>X</td>     *       <td></td>     *       <td></td>     *       <td></td>     *       <td></td>     *     </tr>     *     <tr>     *       <td>{@link DatatypeConstants#GDAY}</td>     *       <td></td>     *       <td></td>     *       <td>X</td>     *       <td></td>     *       <td></td>     *       <td></td>     *     </tr>     *   </tbody>     * </table>     *      * @throws java.lang.IllegalStateException if the combination of set fields     *    does not match one of the eight defined XML Schema builtin      *    date/time datatypes.     * @return One of the following class constants:     *   {@link DatatypeConstants#DATETIME},     *   {@link DatatypeConstants#TIME},     *   {@link DatatypeConstants#DATE},     *   {@link DatatypeConstants#GYEARMONTH},     *   {@link DatatypeConstants#GMONTHDAY},     *   {@link DatatypeConstants#GYEAR},     *   {@link DatatypeConstants#GMONTH} or     *   {@link DatatypeConstants#GDAY}.     */    public QName getXMLSchemaType() {    	    	// DATETIME    	if (year != DatatypeConstants.FIELD_UNDEFINED    		&& month != DatatypeConstants.FIELD_UNDEFINED			&& day != DatatypeConstants.FIELD_UNDEFINED			&& hour != DatatypeConstants.FIELD_UNDEFINED			&& minute != DatatypeConstants.FIELD_UNDEFINED			&& second != DatatypeConstants.FIELD_UNDEFINED) {    		return DatatypeConstants.DATETIME;    	}    	// D

⌨️ 快捷键说明

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