001    package org.hackystat.sensor.ant.util;
002    
003    import java.util.GregorianCalendar;
004    
005    import javax.xml.datatype.DatatypeConfigurationException;
006    import javax.xml.datatype.DatatypeFactory;
007    import javax.xml.datatype.XMLGregorianCalendar;
008    
009    import org.apache.tools.ant.BuildException;
010    
011    /**
012     * Utilities to convert times represented as a long to other formats.
013     * 
014     * @author jsakuda
015     *
016     */
017    public final class LongTimeConverter {
018      
019      /** Private constructor for utility class. */
020      private LongTimeConverter() {
021        // do nothing
022      }
023      
024      /**
025       * Converts a time represented in a long to a XmlGregorianCalendar.
026       * 
027       * @param timeInMillis The time to convert in milliseconds.
028       * @return Returns the time passed in as a <code>XmlGregorianCalendar</code>.
029       */
030      public static XMLGregorianCalendar convertLongToGregorian(long timeInMillis) {
031        DatatypeFactory factory = null;
032        try {
033          factory = DatatypeFactory.newInstance();
034          GregorianCalendar calendar = new GregorianCalendar();
035          calendar.setTimeInMillis(timeInMillis);
036          return factory.newXMLGregorianCalendar(calendar);
037        }
038        catch (DatatypeConfigurationException e) {
039          throw new BuildException("Error creating DatatypeFactory used for converting tstamp.", e);
040        }
041      }
042    }