Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long To XMLGregorianCalendar and back to Long

Tags:

java

date

jaxb

I am trying to convert from millisecond time stamp to XMLGregorianCalendar and back, but I seem to be getting wrong results. Am I doing something wrong? It seems I am gaining days.

    // Time stamp   01-Jan-0001 00:00:00.000
    Long ts = -62135740800000L;
    System.out.println(ts);
    System.out.println(new Date(ts)); // Sat Jan 01 00:00:00 PST 1 .. Cool!

    // to Gregorian Calendar
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(ts);

    // to XML Gregorian Calendar
    XMLGregorianCalendar xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);

    // back to GC
    GregorianCalendar gc2 = xc.toGregorianCalendar();

    // to Timestamp
    Long newTs = gc2.getTimeInMillis();
    System.out.println(newTs);    //  -62135568000000  .. uh?
    System.out.println(new Date(newTs));  // Mon Jan 03 00:00:00 PST 1  .. where did the extra days come from?
like image 444
JD Frias Avatar asked Oct 12 '12 21:10

JD Frias


People also ask

What is XMLGregorianCalendar format?

The Java XMLGregorianCalendar class, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes and is required to use the XML format.

What is the use of XMLGregorianCalendar?

2. XMLGregorianCalendar. The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.

How do I create an instance of XMLGregorianCalendar?

Create a new XMLGregorianCalendar by parsing the String as a lexical representation. Create a Java representation of XML Schema builtin datatype date or g* . Create a Java instance of XML Schema builtin datatype time. Create a Java instance of XML Schema builtin datatype time .


1 Answers

Interesting - it works fine for values down to (about) -10000000000000L (and positive values) but larger negative values become inconsistent.

If you print out gc, xc, and gc2, you can see where the problem arises (the conversion from XMLGregorianCalendar to GregorianCalendar

gc:  java.util.GregorianCalendar[time=-62135740800000 ... DAY_OF_WEEK=7
xc:  0001-01-01T08:00:00.000Z
gc2: java.util.GregorianCalendar[time=? ... DAY_OF_WEEK=5

If you print out the fields of xc, you get 1,1,1.

    System.out.println(xc.getYear());
    System.out.println(xc.getMonth());
    System.out.println(xc.getDay());

For gc2, you get 1,0,1 (which matches xc, because months are zero-based in GregorianCalendar)

    System.out.println(gc2.get(gc2.YEAR));
    System.out.println(gc2.get(gc2.MONTH));
    System.out.println(gc2.get(gc2.DAY_OF_MONTH));

However, adding these 3 println calls changes the output from printing out gc2! - the time=? output from gc2 changes to time=-62135568000000 - so some calculation has been triggered by querying the GregorianCalendar object; the areFieldsSet property also changes from false to true.

The timezones of the two GregorianCalendars are different, but this does not account for the error, which persists even if you set explicit TimeZone and Locale.

like image 121
DNA Avatar answered Oct 20 '22 23:10

DNA