DatatypeConverter { public static String printDateTime(XMLGregorianCalendar calendar) { String result; TimeZone tz = calendar.getTimeZone(DatatypeConstants.FIELD_UNDEFINED); if ( tz.hasSameRules(UTC_TIMEZONE) ) { result = javax.xml.bind.DatatypeConverter.printDateTime(calendar.toGregorianCalendar()); } else { Date utcDateTime = calendar.toGregorianCalendar().getTime(); GregorianCalendar utcCalendar = (GregorianCalendar)GregorianCalendar.getInstance(TimeZone.getTimeZone("Etc/UTC")); utcCalendar.setTime(utcDateTime); result = javax.xml.bind.DatatypeConverter.printDateTime(utcCalendar); } return result; }  static XMLGregorianCalendar parseDateTime(String value); static String printDateTime(XMLGregorianCalendar calendar);  }
@Test public void noMilliseconds() throws DatatypeConfigurationException { GregorianCalendar wellKnownDate = (GregorianCalendar)GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); wellKnownDate.set(1957, 6, 8, 15, 30, 0); wellKnownDate.set(Calendar.MILLISECOND, 0); final XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(wellKnownDate); final String formattedDateString = DatatypeConverter.printDateTime(calendar); Assert.assertEquals("NCIP-formatted date without milliseconds", "1957-07-08T15:30:00Z", formattedDateString); }
@Test public void withMilliseconds() throws DatatypeConfigurationException { GregorianCalendar wellKnownDate = (GregorianCalendar)GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC")); wellKnownDate.set(1957, 6, 8, 15, 30, 0); wellKnownDate.set(Calendar.MILLISECOND, 17); final XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(wellKnownDate); final String formattedDateString = DatatypeConverter.printDateTime(calendar); Assert.assertEquals("NCIP-formatted date with milliseconds", "1957-07-08T15:30:00.017Z", formattedDateString); }