Long To XMLGregorianCalendar and back to Long

Posted by JD. on Stack Overflow See other posts from Stack Overflow or by JD.
Published on 2012-10-12T21:08:03Z Indexed on 2012/10/12 21:37 UTC
Read the original article Hit count: 190

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about date