View Javadoc

1   /*
2    * $Id: DateTimeFormat.java [15-May-2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.util;
35  
36  import java.text.ParseException;
37  import java.text.SimpleDateFormat;
38  import java.util.Date;
39  import java.util.TimeZone;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /***
45   * Defines a format for all iCalendar date-times.
46   * @author benfortuna
47   */
48  public final class DateTimeFormat {
49  
50      private static final String DEFAULT_PATTERN = "yyyyMMdd'T'HHmmss";
51  
52      private static final String UTC_PATTERN = "yyyyMMdd'T'HHmmss'Z'";
53  
54      private static Log log = LogFactory.getLog(DateTimeFormat.class);
55  
56      private static DateTimeFormat instance = new DateTimeFormat();
57  
58      private java.text.DateFormat defaultFormat;
59  
60      private java.text.DateFormat utcFormat;
61  
62      /***
63       * Constructor made private to enforce singleton.
64       */
65      private DateTimeFormat() {
66          defaultFormat = new SimpleDateFormat(DEFAULT_PATTERN);
67  
68          utcFormat = new SimpleDateFormat(UTC_PATTERN);
69          utcFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
70      }
71  
72      /***
73       * @return Returns the instance.
74       */
75      public static DateTimeFormat getInstance() {
76          return instance;
77      }
78  
79      /***
80       * Returns a string representation of the specified
81       * date in UTC format.
82       * @param date a date to format
83       * @return a string representation of the specified date
84       */
85      public String format(final Date date) {
86          return format(date, true);
87      }
88  
89      /***
90       * Returns a string representation of the specified
91       * date.
92       * @param date a date to format
93       * @param utc indicates whether to format as UTC time
94       * @return a string representation of the specified date
95       */
96      public String format(final Date date, final boolean utc) {
97          if (utc) {
98              return utcFormat.format(date);
99          }
100 
101         return defaultFormat.format(date);
102     }
103 
104     /***
105      * @param string a string representation of a date-time
106      * @return a date parsed from the specified string representation
107      * @throws java.text.ParseException thrown if the specified string
108      * is not a valid representation of a date-time
109      */
110     public Date parse(final String string) throws ParseException {
111         try {
112             return utcFormat.parse(string);
113         }
114         catch (ParseException pe) {
115             return defaultFormat.parse(string);
116         }
117     }
118 }