Code example for XMLGregorianCalendar

Methods: add, clone, getDay, getHour, getMinute, getMonth, getSecond, getYear, setTimezone

0
	 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p> 
	 */ 
	private XMLGregorianCalendar normalizeToTimezone(XMLGregorianCalendar cal, int timezone) {
 
	int minutes = timezone;    	
	XMLGregorianCalendar result = (XMLGregorianCalendar) cal.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) {
	   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 = normalizeToTimezone(this, getTimezone());
	} 
	return gc.getYear() + gc.getMonth() + gc.getDay() + 
	    gc.getHour() + gc.getMinute() + gc.getSecond();
    } 
     
     
    /** 
     * <p>Constructs a new XMLGregorianCalendar object by