public static String getUtcTimeAsIso8601 ( Calendar cal ) { try { if ( cal == null ) { return DatatypeFactory . newInstance ( ) . newXMLGregorianCalendar ( new GregorianCalendar ( TimeZone . getTimeZone ( "UTC" ) ) ) . toXMLFormat ( ) . replaceAll ( "\\.[0-9]{3}" , "" ) ; } GregorianCalendar suppliedDateCalendar = new GregorianCalendar ( TimeZone . getTimeZone ( "UTC" ) ) ; suppliedDateCalendar . setTimeInMillis ( cal . getTimeInMillis ( ) ) ; return DatatypeFactory . newInstance ( ) . newXMLGregorianCalendar ( suppliedDateCalendar ) . toXMLFormat ( ) . replaceAll ( "\\.[0-9]{3}" , "" ) ; } catch ( DatatypeConfigurationException e ) { SimpleDateFormat xmlDateUtc = new SimpleDateFormat ( "yyyy-MM-dd'T'HH:mm:ss'Z'" ) ; xmlDateUtc . setTimeZone ( TimeZone . getTimeZone ( "UTC" ) ) ; return xmlDateUtc . format ( Calendar . getInstance ( ) ) ; } }
Formats the given Calendar to an ISO - 8601 compliant string without milliseconds .