Java Code Examples for javax.xml.datatype.XMLGregorianCalendar#setTimezone()

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#setTimezone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateUtils.java    From training with MIT License 6 votes vote down vote up
public static XMLGregorianCalendar extractXmlTime(Date timestamp) {
	if (timestamp == null) {
		return null;
	}
	XMLGregorianCalendar time = null;
	try {
		GregorianCalendar endCalendar = new GregorianCalendar();
		endCalendar.setTime(timestamp);
		time = DatatypeFactory.newInstance().newXMLGregorianCalendar(endCalendar);
		time.setDay(DatatypeConstants.FIELD_UNDEFINED);
		time.setMonth(DatatypeConstants.FIELD_UNDEFINED);
		time.setYear(DatatypeConstants.FIELD_UNDEFINED);
		time.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
		time.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return time;
}
 
Example 2
Source File: XMLGregorianCalendarImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <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;
}
 
Example 3
Source File: XMLGregorianCalendarImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <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;
}
 
Example 4
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <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;
}
 
Example 5
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <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;
}
 
Example 6
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <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;
}
 
Example 7
Source File: AbstractNetSuiteTestBase.java    From components with Apache License 2.0 6 votes vote down vote up
protected static XMLGregorianCalendar composeDateTime() throws Exception {
    DateTime dateTime = DateTime.now();

    XMLGregorianCalendar xts = datatypeFactory.newXMLGregorianCalendar();
    xts.setYear(dateTime.getYear());
    xts.setMonth(dateTime.getMonthOfYear());
    xts.setDay(dateTime.getDayOfMonth());
    xts.setHour(dateTime.getHourOfDay());
    xts.setMinute(dateTime.getMinuteOfHour());
    xts.setSecond(dateTime.getSecondOfMinute());
    xts.setMillisecond(dateTime.getMillisOfSecond());
    xts.setTimezone(dateTime.getZone().toTimeZone().getRawOffset() / 60000);

    return xts;

}
 
Example 8
Source File: XMLGregorianCalendarImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
     * <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;
}
 
Example 9
Source File: XMLGregorianCalendarImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * <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;
}
 
Example 10
Source File: XMLGregorianCalendarImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <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;
}
 
Example 11
Source File: XMLGregorianCalendarImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example 12
Source File: DateUtils.java    From training with MIT License 6 votes vote down vote up
public static XMLGregorianCalendar extractXmlDate(Date timestamp) {
	if (timestamp == null) {
		return null;
	}
	XMLGregorianCalendar date = null;
	try {
		GregorianCalendar startDate = new GregorianCalendar();
		startDate.setTime(timestamp);
		date = DatatypeFactory.newInstance().newXMLGregorianCalendar(startDate);
		date.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
		date.setTime(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
		date.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return date;
}
 
Example 13
Source File: XMLGregorianCalendarImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example 14
Source File: SesameQueryTest.java    From anno4j with Apache License 2.0 5 votes vote down vote up
public void testXmlCalendarZ() throws Exception {
	XMLGregorianCalendar xcal = data.newXMLGregorianCalendar();
	xcal.setYear(2007);
	xcal.setMonth(11);
	xcal.setDay(6);
	xcal.setTimezone(OFFSET);
	ObjectQuery query = manager.prepareObjectQuery(SELECT_BY_DATE);
	query.setObject("date", xcal);
	List list = query.evaluate().asList();
	assertEquals(7, list.size());
}
 
Example 15
Source File: TestXMLGregorianCalendarTimeZone.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void test(int offsetMinutes)
        throws DatatypeConfigurationException {
    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().
            newXMLGregorianCalendar();
    calendar.setTimezone(60 + offsetMinutes);
    TimeZone timeZone = calendar.getTimeZone(DatatypeConstants.FIELD_UNDEFINED);
    String expected = (offsetMinutes < 10 ? "GMT+01:0" : "GMT+01:")
            + offsetMinutes;
    if (!timeZone.getID().equals(expected)) {
        throw new RuntimeException("Test failed: expected timezone: " +
                expected + " Actual: " + timeZone.getID());
    }
}
 
Example 16
Source File: XMLGregorianCalendarToDateTimeConverter.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public XMLGregorianCalendar convertToDatum(Object timestamp) {
    if (timestamp == null) {
        return null;
    }

    long timestampMillis;
    if (timestamp instanceof Long) {
        timestampMillis = ((Long) timestamp).longValue();
    } else if (timestamp instanceof Date) {
        timestampMillis = ((Date) timestamp).getTime();
    } else {
        throw new IllegalArgumentException("Unsupported Avro timestamp value: " + timestamp);
    }

    MutableDateTime dateTime = new MutableDateTime();
    dateTime.setMillis(timestampMillis);

    XMLGregorianCalendar xts = datatypeFactory.newXMLGregorianCalendar();
    xts.setYear(dateTime.getYear());
    xts.setMonth(dateTime.getMonthOfYear());
    xts.setDay(dateTime.getDayOfMonth());
    xts.setHour(dateTime.getHourOfDay());
    xts.setMinute(dateTime.getMinuteOfHour());
    xts.setSecond(dateTime.getSecondOfMinute());
    xts.setMillisecond(dateTime.getMillisOfSecond());
    xts.setTimezone(dateTime.getZone().toTimeZone().getOffset(dateTime.getMillis()) / 60000);

    return xts;
}
 
Example 17
Source File: TestXMLGregorianCalendarTimeZone.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void test(int offsetMinutes)
        throws DatatypeConfigurationException {
    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().
            newXMLGregorianCalendar();
    calendar.setTimezone(60 + offsetMinutes);
    TimeZone timeZone = calendar.getTimeZone(DatatypeConstants.FIELD_UNDEFINED);
    String expected = (offsetMinutes < 10 ? "GMT+01:0" : "GMT+01:")
            + offsetMinutes;
    if (!timeZone.getID().equals(expected)) {
        throw new RuntimeException("Test failed: expected timezone: " +
                expected + " Actual: " + timeZone.getID());
    }
}
 
Example 18
Source File: FATCAMetadata.java    From IDES-Data-Preparation-Java with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
protected XMLGregorianCalendar genTaxYear(int year) {
	XMLGregorianCalendar taxyear = new XMLGregorianCalendarImpl(new GregorianCalendar());
	taxyear.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setTime(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setDay(DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setMonth(DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setYear(year);
	return taxyear;
}
 
Example 19
Source File: Marshalling.java    From proxymusic with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Annotate the scorePartwise tree with information about MusicXML version and, if
 * so desired, with signature composed of ProxyMusic version and date of marshalling.
 *
 * @param scorePartwise   the tree to annotate
 * @param injectSignature if true, ProxyMusic information is added
 */
private static void annotate (final ScorePartwise scorePartwise,
                              final boolean injectSignature)
        throws DatatypeConfigurationException
{
    // Predefined factory for all proxymusic elements
    ObjectFactory factory = new ObjectFactory();

    // Inject version
    scorePartwise.setVersion(ProgramId.VERSION);

    // Inject signature if so desired
    if (injectSignature) {
        // Identification
        Identification identification = scorePartwise.getIdentification();

        if (identification == null) {
            identification = factory.createIdentification();
            scorePartwise.setIdentification(identification);
        }

        // Encoding
        Encoding encoding = identification.getEncoding();

        if (encoding == null) {
            encoding = factory.createEncoding();
            identification.setEncoding(encoding);
        }

        // [Encoding]/Software (only if ProxyMusic is not already listed there)
        List<JAXBElement<?>> list = encoding.getEncodingDateOrEncoderOrSoftware();
        final String programName = ProgramId.NAME + " ";

        for (Iterator<JAXBElement<?>> it = list.iterator(); it.hasNext();) {
            JAXBElement<?> element = it.next();

            if (element.getName().getLocalPart().equals("software")) {
                Object obj = element.getValue();

                if (obj instanceof String && ((String) obj).startsWith(programName)) {
                    // Remove it
                    it.remove();

                    break;
                }
            }
        }

        list.add(
                factory.createEncodingSoftware(
                        ProgramId.NAME + " " + ProgramId.VERSION + "." + ProgramId.REVISION));

        // [Encoding]/EncodingDate (overwrite any existing date)
        for (Iterator<JAXBElement<?>> it = list.iterator(); it.hasNext();) {
            if (it.next().getName().getLocalPart().equals("encoding-date")) {
                it.remove();

                break;
            }
        }

        // Use date without time information (patch by lasconic)
        // Output:     2012-05-03
        // instead of: 2012-05-03T16:17:51.250+02:00
        XMLGregorianCalendar gc = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(
                        new GregorianCalendar());
        gc.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
        gc.setTime(
                DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED);
        list.add(factory.createEncodingEncodingDate(gc));
    }
}
 
Example 20
Source File: XmlUtilities.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Trims the time components of the given calendar if their values are zero, or leaves
 * them unchanged otherwise (except for milliseconds). More specifically:
 *
 * <ul>
 *   <li>If the {@code force} argument is {@code false}, then:
 *     <ul>
 *       <li>If every time components (hour, minute, seconds and milliseconds) are zero, set
 *           them to {@code FIELD_UNDEFINED} in order to prevent them from being formatted
 *           at XML marshalling time. Then returns {@code true}.</li>
 *       <li>Otherwise returns {@code false}. But before doing so, still set the milliseconds
 *           to {@code FIELD_UNDEFINED} if its value was 0.</li>
 *     </ul></li>
 *   <li>Otherwise (if the {@code force} argument is {@code false}), then the temporal
 *       part is set to {@code FIELD_UNDEFINED} unconditionally and this method returns
 *       {@code true}.</li>
 * </ul>
 *
 * <strong>WARNING: The timezone information may be lost!</strong> This method is used mostly
 * when the Gregorian Calendar were created from a {@link Date}, in which case we don't know
 * if the time is really 0 or just unspecified. This method should be invoked only when we
 * want to assume that a time of zero means "unspecified".
 *
 * <p>This method will be deprecated after we implemented ISO 19108 in SIS.</p>
 *
 * @param  gc     the date to modify in-place.
 * @param  force  {@code true} for forcing the temporal components to be removed without any check.
 * @return {@code true} if the time part has been completely removed, {@code false} otherwise.
 */
public static boolean trimTime(final XMLGregorianCalendar gc, final boolean force) {
    if (force || gc.getMillisecond() == 0) {
        gc.setMillisecond(FIELD_UNDEFINED);
        if (force || (gc.getHour() == 0 && gc.getMinute() == 0 && gc.getSecond() == 0)) {
            gc.setHour(FIELD_UNDEFINED);
            gc.setMinute(FIELD_UNDEFINED);
            gc.setSecond(FIELD_UNDEFINED);
            gc.setTimezone(FIELD_UNDEFINED);
            return true;
        }
    }
    return false;
}