Code example for XMLGregorianCalendar

Methods: add, clone, setMillisecond, setTimezone

0
 
        XMLGregorianCalendar normalized = normalizeToTimezone(this, 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(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;
	} 
 
    /**  
     *