Converting from Instant to XMLGregorianCalendar and vice-versa without losing precision

Christophe Broeckx

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
Ole V.V.

Why is this test not passing?

Your XMLGregorianCalendar has got the full precision of your Instant. It’s your conversion back to Instant that is lossy. You are converting to GregorianCalendar through the call to .toGregorianCalendar(). A GreogrianCalendar only has got millisecond precision, so this is where you lose precision.

How to fix it?

Just as you go through a String for your first conversion, you may do the same for the opposite conversion:

    Instant convertedInstant = Instant.parse(xmlGregorianCalendar.toString());

Now your test passes (it did on my Java 9).

Concluding remarks

Instant has got precision up to 9 decimals on the seconds (so nanoseconds). XMLGregorianCalendar has got virtually infinite precision. So if you have converted an Instant to an XMLGregorianCalendar, you know that it hasn’t got precision beyond 9 decimals, so you can safely convert it back. If on the other hand from somewhere you should get an XMLGregorianCalendar with still higher precision (10 decimals or more), the conversion I have shown you will fail with a DateTimeParseException.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting base of floating point number without losing precision

Converting Secret Key into a String and Vice Versa

Converting java.sql.Timestamp to java.util.Calendar without losing precision

"Converting" Numpy arrays to Matlab and vice versa

"Converting" Numpy arrays to Matlab and vice versa

losing precision converting from java BigDecimal to double

Converting Strings to encryption keys and vice versa java

Overhead of converting from []byte to string and vice-versa

Converting XDocument to XmlDocument and vice versa

Converting float NaN values from binary form and vice-versa results a mismatch

Converting QString to Ascii value & Vice Versa in Qt

Converting an Armadillo Matrix to an Eigen MatriXd and vice versa

How to parse Decimal from String without losing precision?

Converting Input text from lowercase to uppercase and vice versa in Javascript

Converting String to Byte Array and vice versa

Converting from smaller data type to bigger and vice versa

Converting pandas dataframe to dict and vice versa

Converting Double to Float and vice versa manually

Converting a population on a grid to coordinates, and vice versa

Converting the result of document.querySelector or document.querySelectorAll to a jquery object or vice versa without a reselection?

Converting base 6 to decimal and vice versa in Python?

How to switch from Linux (Ubuntu 18.04) to Windows and vice versa without closing applications and losing data?

Scala: Converting a Double from Scientific Notation without losing precision?

Converting byte[] to json and vice versa without jackson or gson

R - Converting formulae to character strings and vice versa

Converting string to integer and vice versa in Python

Java - Converting String from Scanner to int and vice versa

Converting char to binary strings and vice-versa

Is converting from unsigned char to signed char and vice versa in C89 well defined?

TOP Ranking

HotTag

Archive