10

I want to compute the delta of time, a subtraction, between two XmlGregorianCalendar objects, so as to create a Duration object.

But I haven't found clean ways of performing that subtraction. How would you do it?

entpnerd
  • 10,049
  • 8
  • 47
  • 68
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169

2 Answers2

13

That should be:

DatatypeFactory.newDuration(xgc2.toGregorianCalendar().getTimeInMillis() - xgc1.toGregorianCalendar().getTimeInMillis())
Jerome
  • 8,427
  • 2
  • 32
  • 41
1

The accepted answer only gives the results in millis resolution but XmlGregorianCalendar allows infinite precision. We had to solve the problem for µS resolution. I did it by converting to big decimal and using getFractionalSeconds. see below

 public static BigDecimal convertXMLGregorianToSecondsAndFractionalSeconds(XMLGregorianCalendar xgc){
    long ms = xgc.toGregorianCalendar().getTimeInMillis();
    long secs = ms / 1000l;
    BigDecimal decValue = BigDecimal.valueOf(secs);
    BigDecimal fracSects = xgc.getFractionalSecond();
    decValue = decValue.add(fracSects);
    return decValue;
}

 @Test
public void testSubtraction() throws Exception {
    XMLGregorianCalendar xgc1 =  DatatypeFactory.newInstance().newXMLGregorianCalendar("2015-05-22T16:28:40.317123-04:00");
    XMLGregorianCalendar xgc2 =  DatatypeFactory.newInstance().newXMLGregorianCalendar("2015-05-22T16:28:40.917124-04:00");
    BigDecimal bd1 = MathUtils.convertXMLGregorianToSecondsAndFractionalSeconds(xgc1);
    BigDecimal bd2 = MathUtils.convertXMLGregorianToSecondsAndFractionalSeconds(xgc2);
    BigDecimal result = bd2.subtract(bd1);
    Assert.assertTrue(result.equals(new BigDecimal("0.600001")));
}  
TT.
  • 15,774
  • 6
  • 47
  • 88
Jeff Gaer
  • 351
  • 3
  • 21