This discussion is archived
3 Replies Latest reply: Jun 19, 2011 9:47 PM by 869899 RSS

javax.xml.bind.DatatypeConverter fails to convert very old dates

869899 Newbie
Currently Being Moderated
import javax.xml.bind.DatatypeConverter;

Calendar calDate = DatatypeConverter.parseDate("0001-01-01);
System.out.println(calDate.getTime());
output: Mon Jan 03 00:00:00 EST 1

Calendar calDateTime = DatatypeConverter.parseDateTime("0001-01-01T00:00:00.000);
System.out.println(calDateTime .getTime());
output: Mon Jan 03 00:00:00 EST 1

Calendar calDate = DatatypeConverter.parseDate("1001-01-01);
System.out.println(calDate.getTime());
output: Thu Dec 26 00:00:00 EST 1000


Am I doing something wrong or is this a bug?

Edited by: 866896 on Jun 19, 2011 6:07 PM
  • 1. Re: javax.xml.bind.DatatypeConverter fails to convert very old dates
    869899 Newbie
    Currently Being Moderated
    After much fooling about and finding all sorts of other 3rd party components make the same mistake I discovered this was a GregorianCalendar mistake.
    Since this period isn't covered by the Gregorian Calendar it's out by up to 11 days in 1753.
    This must be a bug as the compliance with this standard http://www.w3.org/TR/xmlschema-2/ states that:
    "Each such object also has one decimal-valued method or computed property, timeOnTimeline, whose value is always a decimal number; the values are dimensioned in seconds, the integer 0 is 0001-01-01T00:00:00"
    Only other dattime representations use "Gregorian algorithm as modified for leap-seconds"
    So I assume that the DatatypeConverter.parseDateTime() method should not be using the Gregorian algorithm.

    Should I report this as a bug?
  • 2. Re: javax.xml.bind.DatatypeConverter fails to convert very old dates
    EJP Guru
    Currently Being Moderated
    I don't follow your logic about it being a GregorianCalendar mistake. The 11 days in 1753 are handled correctly by GregorianCalendar as per its Javadoc, and they don't have anything to do with this problem anyway as you aren't out by 11 days. The Javadoc also states 'However, dates obtained using GregorianCalendar are historically accurate only from March 1, 4 AD onward'. So you can't represent it as a GregorianCalendar problem, because it is out of its domain. DatatypeConverter, yes.
  • 3. Re: javax.xml.bind.DatatypeConverter fails to convert very old dates
    869899 Newbie
    Currently Being Moderated
    You're half right. It's not a GregorianCalendar calendar problem as such, but it is associated with this.
    If I run this method on the date "1753-01-01T00:00:00.000" it is 11 days out.
    The problem only occurs when using the timeInMillis constructor which makes sense in this context.
    Time in milliseconds since 0001-01-01 is different depending on if it's calculated in Gregorian or Julian standards.
    By definition Gregorian dates are continuous, but in 1753, 11 days were removed from September so it's different.
    In contrast, If I calculate the current date in the Julian standard, it's about 11 days different.

    I just think that the XML Parser should conform to the standard and give an accurate date no matter if it's before or after epoc.

    I have a work around. I parse the date myself and break it up into the constituent parts then construct a Calendar with those parts and add the milliseconds.
    Perhaps the parser should do the same?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points