Java 7 and 8 – different XmlGregorianCalendar


package calendar;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.junit.Test;

public class CalendarTest {

    @Test
    public void shouldCreateXmlGregorianCalendar() throws DatatypeConfigurationException {

        // given
        System.setProperty("user.timezone", "America/Chicago");
        Calendar cal = Calendar.getInstance();
        cal.set(2013, 10, 28, 12, 1, 0);
        cal.set(Calendar.MILLISECOND, 100);
        cal.setTimeZone(TimeZone.getTimeZone("CDT"));

        // when
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        calendar.setTime(cal.getTime());
        XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        xmlCal.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
        xmlCal.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);

        // then
        System.out.println(xmlCal.toXMLFormat());
    }
}

Java 8
2013-11-28T13:01:00

Java7
2013-11-28T06:01:00
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s