I had a curious situation at work, where an application sent us XML containing the value "0001-01-01", which was parsed into an instance of XmlGregorianCalendar
. I then realized, the value magically converted into "0001-01-03", the exact amount of 2 days was added.
This happened during the conversion from GregorianCalendar
to Date, which I reproduced as followed:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class Test {
public static void main(String[] args) throws ParseException, DatatypeConfigurationException {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gregCalendar = new GregorianCalendar();
gregCalendar.setTime(dateFormat.parse("0001-01-01"));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregCalendar);
System.out.println("calendar: " + calendar);
System.out.println("date: " + calendar.toGregorianCalendar().getTime());
}
}
Sample output:
calendar: 0001-01-01T00:00:00.000Z
date: Mon Jan 03 00:00:00 GMT 1
The milliseconds differ by the exact amount of 172800000. Does anybody know why?