Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.GregorianCalendar; import java.util.Date; import javax.xml.datatype.DatatypeFactory; public class Main { /** * Convenience method to format a Date as an XML DateTime String. * * @param date * the date to format. * @return the XML representation as a string. */ public static String formatDate(final Date date) { GregorianCalendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(date.getTime()); return formatGregorianCalendar(calendar); } /** * Format a Gregorian Calendar as an XML DateTime String. * * @param calendar * the calendar to format. * @return the XML representation as a string. */ public static String formatGregorianCalendar(final GregorianCalendar calendar) { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar).normalize().toXMLFormat(); } catch (Exception e) { return null; } } }