View Javadoc

1   /*
2    * #%L
3    * Bitrepository Modifying Client
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
24   */
25  package org.bitrepository.common.utils;
26  
27  import java.util.Date;
28  import java.util.GregorianCalendar;
29  
30  import javax.xml.datatype.DatatypeFactory;
31  import javax.xml.datatype.XMLGregorianCalendar;
32  
33  import org.bitrepository.common.ArgumentValidator;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * Utility class for calendar issues. 
39   */
40  public final class CalendarUtils {
41      /** The log.*/
42      private static Logger log = LoggerFactory.getLogger(CalendarUtils.class);
43  
44      /**
45       * Private constructor to prevent instantiation of utility class.
46       */
47      private CalendarUtils() { }
48      
49      /**
50       * Turns a date into a XMLGregorianCalendar.
51       * @param date The date. If the argument is null, then epoch is returned.
52       * @return The XMLGregorianCalendar.
53       */
54      public static XMLGregorianCalendar getXmlGregorianCalendar(Date date) {
55          if(date == null) {
56              log.debug("Cannot convert the date '" + date + "'. Returning epoch instead.");
57              date = new Date(0);
58          }
59          
60          GregorianCalendar gc = new GregorianCalendar();
61          try {
62              gc.setTime(date);
63              return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
64          } catch (Exception e) {
65              throw new IllegalStateException("Could not convert the date '" + date + "' into the xml format.", e);
66          }
67      }
68  
69      /**
70       * Method for easier retrieving the current date in XML format.
71       * @return The current date in XML format
72       */
73      public static XMLGregorianCalendar getXmlGregorianCalendar(GregorianCalendar gregorianCalendar) {
74          try {
75              return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
76          } catch (Exception e) {
77              throw new IllegalStateException("Could not convert the date '" + gregorianCalendar + "' into the xml format.", e);
78          }
79      }
80  
81      /**
82       * Method for easier retrieving the current date in XML format.
83       * @return The current date in XML format
84       */
85      public static XMLGregorianCalendar getNow() {
86          return getXmlGregorianCalendar(new Date());
87      }
88      
89      /**
90       * Method for easier retrieving the date for Epoch (January 1, 1970 00:00:00.000 GMT).
91       * @return Epoch in XMLGregorianCalendar format.
92       */
93      public static XMLGregorianCalendar getEpoch() {
94          return getXmlGregorianCalendar(new Date(0));
95      }
96      
97      /**
98       * Method for easier retrieving the Date for a given time since Epoch in millis.
99       * @param millis The amount of milliseconds since Epoch.
100      * @return The date in XMLGregorianCalendar format.
101      */
102     public static XMLGregorianCalendar getFromMillis(long millis) {
103         return getXmlGregorianCalendar(new Date(millis));
104     }
105     
106     /**
107      * Method for converting a date from the XML calendar type 'XMLGregorianCalendar' to the default java date.
108      * 
109      * @param xmlCal The XML calendar to convert from.
110      * @return The date for the XML calendar converted into the default java date class.
111      */
112     public static Date convertFromXMLGregorianCalendar(XMLGregorianCalendar xmlCal) {
113         ArgumentValidator.checkNotNull(xmlCal, "XMLGregorianCalendar xmlCal");
114         
115         return xmlCal.toGregorianCalendar().getTime();
116     }
117 }