Using, java.time.Instant
and javax.xml.datatype.XMLGregorianCalendar
, I'm trying to convert from one to the other without losing precision.
Why is this test not passing and how to fix it?
class FooTest {
@Test
void shouldConvertBackToSameInstant() throws DatatypeConfigurationException {
Instant initialInstant = Instant.now();
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(String.valueOf(initialInstant));
Instant convertedInstant = xmlGregorianCalendar.toGregorianCalendar().toInstant();
assertEquals(initialInstant, convertedInstant);
}
}
org.opentest4j.AssertionFailedError:
Expected :2021-03-10T08:34:30.700715078Z
Actual :2021-03-10T08:34:30.700Z