View Javadoc

1   /*
2    * #%L
3    * Bitrepository Webclient
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.utils;
23  
24  import java.util.Date;
25  import java.util.GregorianCalendar;
26  import javax.xml.datatype.DatatypeConfigurationException;
27  import javax.xml.datatype.DatatypeFactory;
28  import javax.xml.datatype.XMLGregorianCalendar;
29  
30  /**
31   * Utility class for converting between XMLGregorianCalendar and java.util.Date
32   */
33  public class XMLGregorianCalendarConverter {  
34  
35      /**
36       * Needed to create XMLGregorianCalendar instances
37       */
38      private static DatatypeFactory df = null;
39      static {
40          try {
41              df = DatatypeFactory.newInstance();
42          } catch (DatatypeConfigurationException dce) {
43              throw new IllegalStateException(
44                  "Exception while obtaining DatatypeFactory instance", dce);
45          }
46      }  
47  
48      /**
49       * Converts a java.util.Date into an instance of XMLGregorianCalendar
50       *
51       * @param date Instance of java.util.Date or a null reference
52       * @return XMLGregorianCalendar instance whose value is based upon the
53       *  value in the date parameter. If the date parameter is null then
54       *  this method will simply return null.
55       */
56      public static XMLGregorianCalendar asXMLGregorianCalendar(Date date) {
57          if (date == null) {
58              return null;
59          } else {
60              GregorianCalendar gc = new GregorianCalendar();
61              gc.setTimeInMillis(date.getTime());
62              return df.newXMLGregorianCalendar(gc);
63          }
64      }
65  
66      /**
67       * Converts an XMLGregorianCalendar to an instance of java.util.Date
68       *
69       * @param xgc Instance of XMLGregorianCalendar or a null reference
70       * @return java.util.Date instance whose value is based upon the
71       *  value in the xgc parameter. If the xgc parameter is null then
72       *  this method will simply return null.
73       */
74      public static Date asDate(XMLGregorianCalendar xgc) {
75          if (xgc == null) {
76              return null;
77          } else {
78              return xgc.toGregorianCalendar().getTime();
79          }
80      }
81  }